Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I have survey data in the dataframe. I can use print(list(survey_data.columns.values)) to get headers which are (not listing all 27 columns here but here is a sample)

['Age', 'Gender', 'Country', 'family_history']

I can use the following to get unique values of each column in for loop as follows:

for col in survey_data:

print(survey_data[col].unique())

[37 44 32 31 33 35 39 42 23 29 36 27 46 41 34 30 40 38 50 24 28 26 22 19 25 45 21 43 56 60 54 55 48 20 57 58 47 62 51 49 53 61]

I want to print column headers and unique values as follows

Desired output:

Age = [37 44 32 31 33 35 39 42 23 29 36 27 46 41 34 30 40 38 50 24 28 26 22 19 25 45 21 43 56 60 54 55 48 20 57 58 47 62 51 49 53 61]

Gender = ['F' 'M' 'T']

Country = ['United States' 'Canada' 'United Kingdom' 'Bulgaria' 'France' 'Portugal' 'Switzerland' 'Poland' 'Australia' 'Germany' 'Mexico' 'Brazil' 'Slovenia' 'Costa Rica' 'Austria' 'Ireland' 'India' 'South Africa' 'Russia' 'Italy' 'Netherlands' 'Sweden' 'Colombia' 'Latvia' 'Romania' 'Belgium' 'New Zealand' 'Spain' 'Finland' 'Uruguay' 'Israel' 'Bosnia and Herzegovina' 'Hungary' 'Singapore' 'Japan' 'Nigeria' 'Croatia' 'Norway' 'Thailand' 'Denmark' 'Greece' 'Moldova' 'Georgia' 'China' 'Czech Republic' 'Philippines']

I thought a statement would get me there.

print(list(survey_data.columns.values) for col in survey_data[col].unique())

Instead I get <generator object at 0x0000031EF53F3C80>

1 Answer

0 votes
by (36.8k points)

Please see the below, you can use:

for col in survey_data.columns:

    print(survey_data[col].unique().tolist())

OR

for col in survey_data.columns:

    print(col, survey_data[col].unique().tolist())

OR

for col in survey_data.columns:

    for unique_val in survey_data[col].unique():

        print(col, unique_val)

 Want to be a master in Data Science? Enroll in this Data Scientist

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...