1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Why is my String comparison not working in Python? [duplicate]

Discussão em 'Python' iniciado por Stack, Setembro 17, 2024.

  1. Stack

    Stack Membro Participativo

    I wrote a simple code in Python based on a survey data

    `
    # Load CSV data
    csv_file = 'women_survey.csv'
    data_women = pd.read_csv(csv_file)

    # Convert object to String
    data_women['physical_Assault'] = data_women['physical_Assault'].astype("string")
    w_marital_status = data_women['physical_Assault']

    # print sample data size and type
    print(w_marital_status.shape)
    print(w_marital_status.dtype)
    # person 1's data
    print(w_marital_status[0])

    # Create a new List
    new_list = [""] * 62

    # Read each iteration of survey data and assign String to new List
    for count, content in enumerate(w_marital_status, start=0):
    new_test = content.split(":", 1)[1]
    new_list[count] = new_test
    print(new_test)
    if type(new_test) == str:
    if new_test == "Yes":
    print("This is a Yes")
    else:
    print("This is a No")
    else:
    print("This is not a string.")`


    This code provides the following result (part of it).

    (62,)
    string
    Have you ever been physically assaulted?: No

    No

    This is a No
    No

    This is a No
    No

    This is a No
    No

    This is a No
    Yes

    This is a No


    As shown in the result, for each person, each row contains the entire question "Have you ever been physically assaulted?" along with the appended answer "Yes" or "No"

    I want to store only the answers, "Yes" or "No", in order to later use MatplotLib and graph the data. so I wrote the For loop to assign the answers to a new List. But as you can see, the String comparison never works. I wasn't sure if "new_test" was actually a String, it turns out it is a String, but still the String comparison doesnt work. Im not quite sure what I've done wrong. Also if there is more simple way than my code, please let me know. Thanks.

    Continue reading...

Compartilhe esta Página