Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to put column name into data frame cell with specific conditions in pandas

I have a dataframe like this:

         ADR     WD      EF    INF    SSI   DI
0        1.0    NaN     NaN    NaN    NaN  NaN
1        NaN    NaN     1      1      NaN  NaN
2        NaN    NaN     NaN    NaN    1    NaN
3        NaN    1       1      1      NaN  NaN
4        NaN    1.0     NaN    NaN    NaN  NaN

I want the result to be like this:

[["ADR"],["EF","INF"],["SSI"],["WD","EF","INF"],["WD"]]

As you see the name of the column has been replaced if there is 1 in that column. and all has been put in another array.

I have looked at this post link but it did not help me as the name has changed staticly.

Thanks:)

like image 499
sariii Avatar asked Aug 09 '18 03:08

sariii


People also ask

How do I give column names to pandas DataFrame?

Adding column name to the DataFrame : We can add columns to an existing DataFrame using its columns attribute. Output : Now the DataFrame has column names. Renaming column name of a DataFrame : We can rename the columns of a DataFrame by using the rename() function.

How do I show specific columns in a data frame?

If you have a DataFrame and would like to access or select a specific few rows/columns from that DataFrame, you can use square brackets or other advanced methods such as loc and iloc .


2 Answers

Use:

df1 = df.stack().reset_index()
df1.columns = ['a','b','c']
df1 = df1[df1['c'] == 1]

a = df1.groupby('a')['b'].apply(list).tolist()
print (a)
[['ADR'], ['EF', 'INF'], ['SSI'], ['WD', 'EF', 'INF'], ['WD']]
like image 152
jezrael Avatar answered Oct 19 '22 04:10

jezrael


IIUC dot

df.notnull().dot(df.columns+',').str[:-1].str.split(',').tolist()
Out[753]: [['ADR'], ['EF', 'INF'], ['SSI'], ['WD', 'EF', 'INF'], ['WD']]
like image 5
BENY Avatar answered Oct 19 '22 04:10

BENY