本文介绍了尝试在数据框中删除NaN索引行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用python 2.7.3和Pandas版本0.12.0.
I'm using python 2.7.3 and Pandas version 0.12.0.
我想删除带有NaN索引的行,以便我只有有效的site_id值.
I want to drop the row with the NaN index so that I only have valid site_id values.
print df.head()
special_name
site_id
NaN Banana
OMG Apple
df.drop(df.index[0])
TypeError: 'NoneType' object is not iterable
如果我尝试降低范围,例如:
If I try dropping a range, like this:
df.drop(df.index[0:1])
我收到此错误:
AttributeError: 'DataFrame' object has no attribute 'special_name'
推荐答案
我发现最简单的方法是重置索引,删除NaN,然后再次重置索引.
I've found that the easiest way is to reset the index, drop the NaNs, and then reset the index again.
In [26]: dfA.reset_index()
Out[26]:
index special_name
0 NaN Apple
1 OMG Banana
In [30]: df = dfA.reset_index().dropna().set_index('index')
In [31]: df
Out[31]:
special_name
index
OMG Banana
这篇关于尝试在数据框中删除NaN索引行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!