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:)
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.
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 .
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']]
IIUC dot
df.notnull().dot(df.columns+',').str[:-1].str.split(',').tolist()
Out[753]: [['ADR'], ['EF', 'INF'], ['SSI'], ['WD', 'EF', 'INF'], ['WD']]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With