Reputation: 273
i want to add the prefix '_nan' to columns that are all nan. I have the following code that prints what I want but does not reassign the columns in the actual dataframe and I am not sure why. Does anyone have any ideas why this is happening? Thanks in advance
df = pd.DataFrame({ 'a':[1, 0, 0, 0],
'b':[np.nan, np.nan, np.nan, np.nan],
'c':[np.nan, np.nan, np.nan, np.nan]})
a = df.loc[:,df.isna().all()].columns
df[[*a]] = df[[*a]].add_suffix('_nan')
Upvotes: 0
Views: 893
Reputation: 36450
why this is happening?
After some experiments I found that when you assign pandas.DataFrame
slice to slice of pandas.DataFrame
then pandas
apparently does bother solely about order of columns (given in list
), not their names, consider following example:
import pandas as pd
df_1 = pd.DataFrame({'a':[1,2,3],'b':[4,5,6],'c':[7,8,9]})
df_2 = pd.DataFrame({'x':[10,20,30],'y':[400,500,600]})
df_1[['a','b']] = df_2[['x','y']]
print(df_1)
output
a b c
0 10 400 7
1 20 500 8
2 30 600 9
whilst
...
df_1[['a','b']] = df_2[['y','x']]
print(df_1)
produce
a b c
0 400 10 7
1 500 20 8
2 600 30 9
Upvotes: 0
Reputation: 1875
You can use list comprehension:
df.columns = [x + '_nan' if df[x].isna().all() else x for x in df.columns]
Output:
a b_nan c_nan
0 1 NaN NaN
1 0 NaN NaN
2 0 NaN NaN
3 0 NaN NaN
Upvotes: 1