Merging multiple values into one row in a new column Pandas Python

Umar.H

Greetings Beautiful People!

I'm putting together a visualization for some customer whoops edit survey data. Unfortunately, the data modeling or end to end process throughout is non-existent

I have multiple columns as follows :

    What Role : Teacher, What Role: Engineer, What Role : Doctor
1   Yes,                 Yes,                 No, 
2   No,                  No,                  Yes,
3,  Yes,                 No,                  Yes, 

so, what I want to do is create a new column and convert the Yes' into a new Value which matches the Header, so if doctor is Yes, then it would enter int a new Column:

    What Role?
1   Teacher, Engineer,
2   Doctor,
3   Teacher, Doctor

Could this be done by creating a dictionary then a for loop?

for example:

import pandas as pd

df = pd.read_csv("file.csv")

Dictionary_File = {'What Role?' : 'What Role : Teacher', 
'What Role?': 'What Role : Engineer', 'What Role?' : 'What Role : Doctor'}

for k,v in Dictionary_File.items():
   (df[k] = df[k] == 'Yes', 'Unsure here' + df[v])

df = df.drop(list(Dictonary_File.values()), axis=1)

So when it comes to the for loop I couldn't think or find a way to merge the values into something new (Other than manually changing all the columns Yes into a new value then merging..?)

any help would be much appreciated!

Cheers,

jezrael

You need first remove What Role: by split.

Then by boolean mask df == 'Yes' create joined values by numpy.where

c = df.columns.str.split().str[-1]
s = np.where(df == 'Yes', ['{}, '.format(x) for x in c], '')
print (s)
[['Teacher, ' 'Engineer, ' '']
 ['' '' 'Doctor, ']
 ['Teacher, ' '' 'Doctor, ']]

df['new'] = pd.Series([''.join(x).strip(', ') for x in s], index=df.index)
print (df)
  What Role : Teacher What Role : Engineer What Role : Doctor  \
1                 Yes                  Yes                 No   
2                  No                   No                Yes   
3                 Yes                   No                Yes   

                 new  
1  Teacher, Engineer  
2             Doctor  
3    Teacher, Doctor  

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Assigning multiple column values in a single row of pandas DataFrame, in one line

From Dev

Merge multiple column values into one column in python pandas

From Dev

Merge multiple values of a column after group by into one column in python pandas

From Dev

Merging multiple rows into one row

From Dev

Python pandas column merging

From Dev

How to return one row with multiple column values?

From Dev

Merging pandas columns into a new column

From Dev

Merging two data frames based on row values in python pandas

From Java

pandas create new column based on values from other columns / apply a function of multiple columns, row-wise

From Dev

Python3.7 Pandas1.0.1 Dataframe - Calculate sum of column within a range and regroup as one new row?

From Dev

Python Pandas multiple occurrences of a row in dataframe 1 into new column of dataframe 2

From Dev

Map value from one row as a new column in pandas

From Dev

new column based on row and column conditions pandas python

From Dev

Merging two pandas dataframe with column values

From Dev

sql query get multiple values from same column for one row

From Dev

Excel - Return multiple matching values from a column, horizontally in one row

From Dev

Displaying multiple values from same column in one row in SQL

From Dev

How to do multiple column conversions on a Pandas Row in a DF in one pass

From Dev

Merging values for one column and summing up the values in the second column

From Dev

Pandas Set multiple column and row values to nan based on another dataframe

From Dev

Python pandas: Setting row values in Pandas using data of that column

From Dev

Merging pandas dataframes on multiple conditions (python/pandas)

From Dev

Merging pandas dataframes on multiple conditions (python/pandas)

From Dev

Create a new column in pandas based on values in multiple columns and the same condition

From Dev

Merging multiple rows with multiple factors to create a new row in a dataset

From Dev

Moving row values contains specific string to new column in Python

From Dev

Record values of one column based on another column: Python & Pandas

From Dev

Python Pandas - Dataset with many columns - want to iterate over each column, add row values to new list only from fields that are not null

From Dev

Merging values in different columns into one in python list

Related Related

  1. 1

    Assigning multiple column values in a single row of pandas DataFrame, in one line

  2. 2

    Merge multiple column values into one column in python pandas

  3. 3

    Merge multiple values of a column after group by into one column in python pandas

  4. 4

    Merging multiple rows into one row

  5. 5

    Python pandas column merging

  6. 6

    How to return one row with multiple column values?

  7. 7

    Merging pandas columns into a new column

  8. 8

    Merging two data frames based on row values in python pandas

  9. 9

    pandas create new column based on values from other columns / apply a function of multiple columns, row-wise

  10. 10

    Python3.7 Pandas1.0.1 Dataframe - Calculate sum of column within a range and regroup as one new row?

  11. 11

    Python Pandas multiple occurrences of a row in dataframe 1 into new column of dataframe 2

  12. 12

    Map value from one row as a new column in pandas

  13. 13

    new column based on row and column conditions pandas python

  14. 14

    Merging two pandas dataframe with column values

  15. 15

    sql query get multiple values from same column for one row

  16. 16

    Excel - Return multiple matching values from a column, horizontally in one row

  17. 17

    Displaying multiple values from same column in one row in SQL

  18. 18

    How to do multiple column conversions on a Pandas Row in a DF in one pass

  19. 19

    Merging values for one column and summing up the values in the second column

  20. 20

    Pandas Set multiple column and row values to nan based on another dataframe

  21. 21

    Python pandas: Setting row values in Pandas using data of that column

  22. 22

    Merging pandas dataframes on multiple conditions (python/pandas)

  23. 23

    Merging pandas dataframes on multiple conditions (python/pandas)

  24. 24

    Create a new column in pandas based on values in multiple columns and the same condition

  25. 25

    Merging multiple rows with multiple factors to create a new row in a dataset

  26. 26

    Moving row values contains specific string to new column in Python

  27. 27

    Record values of one column based on another column: Python & Pandas

  28. 28

    Python Pandas - Dataset with many columns - want to iterate over each column, add row values to new list only from fields that are not null

  29. 29

    Merging values in different columns into one in python list

HotTag

Archive