Joy*_*ove 8 python indexing nan dataframe pandas
在标题行为NaN的数据框中删除列的最pythonic地方是什么?最好是在位.
列中可能有也可能没有数据.
df = pd.DataFrame({'col1': [1,2,np.NaN], 'col2': [4,5,6], np.NaN: [7,np.NaN,9]})
df.dropna(axis='columns', inplace=True)
Run Code Online (Sandbox Code Playgroud)
在查看列中的数据时不这样做.
想要输出
df = pd.DataFrame({'col1': [1,2,np.NaN], 'col2': [4,5,6]})
Run Code Online (Sandbox Code Playgroud)
在此先感谢您的回复.
试试吧
df.drop(np.nan, axis=1, inplace=True)
Run Code Online (Sandbox Code Playgroud)
但是,如果'无标题'包含None,那么jpp的答案将在一次完美运行.即使你有多个np.nan标题,我也不知道如何制作df.drop作品.
您可以使用pd.Index.dropna:
df = df[df.columns.dropna()]
print(df)
col1 col2
0 1.0 4
1 2.0 5
2 NaN 6
Run Code Online (Sandbox Code Playgroud)