Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:函数dropna(pandas dataframe)可以删除带有空白单元格的行吗?_Python_Pandas - Fatal编程技术网

Python:函数dropna(pandas dataframe)可以删除带有空白单元格的行吗?

Python:函数dropna(pandas dataframe)可以删除带有空白单元格的行吗?,python,pandas,Python,Pandas,如何删除带有空白单元格的行 dftest Out[284]: aaa aaa_f aaa_rw test Period 19931115 26.853 26.9142 26.873 26.873 19931116 26.861 26.8453 26.853 26.853 19931117 26.903 26.8528 26.861 26.861 1993

如何删除带有空白单元格的行

dftest
Out[284]: 
             aaa    aaa_f  aaa_rw    test
Period                                       
19931115  26.853  26.9142  26.873  26.873
19931116  26.861  26.8453  26.853  26.853
19931117  26.903  26.8528  26.861  26.861
19931118  26.880           26.903  26.903
19931119  26.881           26.880  26.880
19931122  26.900           26.881  26.881
19931123  26.899           26.900  26.900
如果我直接调用函数dropna(),这将不起作用


您希望传递
how='any'
,它将删除包含任何nan单元格的所有行

df.dropna(how='any')
输出:

             aaa    aaa_f  aaa_rw    test
Period                                   
19931115  26.853  26.9142  26.873  26.873
19931116  26.861  26.8453  26.853  26.853
19931117  26.903  26.8528  26.861  26.861
     Period     aaa    aaa_f  aaa_rw    test
0  19931115  26.853  26.9142  26.873  26.873
1  19931116  26.861  26.8453  26.853  26.853
2  19931117  26.903  26.8528  26.861  26.861
如果只想将那些
nan
放在特定列中,则可以传递
子集

df.dropna(subset=['aaa_f'])

<强> Update:在我看来,您的列可能是字符串类型,空白值表示空字符串<代码> '/COD>,因此您希望

df[df['aaa_f'].ne('')]


使用pandas.DataFrame.replace和
dropna

df.replace("", np.nan).dropna()
输出:

             aaa    aaa_f  aaa_rw    test
Period                                   
19931115  26.853  26.9142  26.873  26.873
19931116  26.861  26.8453  26.853  26.853
19931117  26.903  26.8528  26.861  26.861
     Period     aaa    aaa_f  aaa_rw    test
0  19931115  26.853  26.9142  26.873  26.873
1  19931116  26.861  26.8453  26.853  26.853
2  19931117  26.903  26.8528  26.861  26.861

您必须使用
in place
选项

df.dropna(inplace=True)
或者重新分配您的
df

df = df.dropna()

谢谢,但它不起作用。dftest.dropna(how='any')Out[291]:twd twd_f twd_rw测试周期19931115 26.853 26.9142 26.873 26.873 19931116 26.861 26.8453 26.853 19931117 26.903 26.8528 26.861 26.861 19931118 26.880 26.903 26.903 19931119 26.8826.880 26.880 19931122 26.900 26.881 26.881 26.8811993112326.89926.90026.900您的空白单元格是
NaN
还是字符串
'