Python 从熊猫数据框中删除具有空值的行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44548721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove row with null value from pandas data frame
提问by ryan pickles
I'm trying to remove a row from my data frame in which one of the columns has a value of null. Most of the help I can find relates to removing NaN values which hasn't worked for me so far.
我试图从我的数据框中删除一行,其中一列的值为 null。我能找到的大部分帮助都与删除 NaN 值有关,这些值到目前为止对我不起作用。
Here I've created the data frame:
在这里,我创建了数据框:
# successfully crated data frame
df1 = ut.get_data(symbols, dates) # column heads are 'SPY', 'BBD'
# can't get rid of row containing null val in column BBD
# tried each of these with the others commented out but always had an
# error or sometimes I was able to get a new column of boolean values
# but i just want to drop the row
df1 = pd.notnull(df1['BBD']) # drops rows with null val, not working
df1 = df1.drop(2010-05-04, axis=0)
df1 = df1[df1.'BBD' != null]
df1 = df1.dropna(subset=['BBD'])
df1 = pd.notnull(df1.BBD)
# I know the date to drop but still wasn't able to drop the row
df1.drop([2015-10-30])
df1.drop(['2015-10-30'])
df1.drop([2015-10-30], axis=0)
df1.drop(['2015-10-30'], axis=0)
with pd.option_context('display.max_row', None):
print(df1)
Here is my output:
这是我的输出:
Can someone please tell me how I can drop this row, preferably both by identifying the row by the null value and how to drop by date?
有人可以告诉我如何删除这一行,最好是通过空值识别行以及如何按日期删除?
I haven't been working with pandas very long and I've been stuck on this for an hour. Any advice would be much appreciated.
我和熊猫一起工作的时间不长,我已经坚持了一个小时。任何建议将不胜感激。
回答by Marjan Moderc
This should do the work:
这应该做的工作:
df = df.dropna(how='any',axis=0)
It will erase every row(axis=0) that has "any" Null value in it.
它将擦除其中包含“任何”空值的每一行(轴=0)。
EXAMPLE:
例子:
#Recreate random DataFrame with Nan values
df = pd.DataFrame(index = pd.date_range('2017-01-01', '2017-01-10', freq='1d'))
# Average speed in miles per hour
df['A'] = np.random.randint(low=198, high=205, size=len(df.index))
df['B'] = np.random.random(size=len(df.index))*2
#Create dummy NaN value on 2 cells
df.iloc[2,1]=None
df.iloc[5,0]=None
print(df)
A B
2017-01-01 203.0 1.175224
2017-01-02 199.0 1.338474
2017-01-03 198.0 NaN
2017-01-04 198.0 0.652318
2017-01-05 199.0 1.577577
2017-01-06 NaN 0.234882
2017-01-07 203.0 1.732908
2017-01-08 204.0 1.473146
2017-01-09 198.0 1.109261
2017-01-10 202.0 1.745309
#Delete row with dummy value
df = df.dropna(how='any',axis=0)
print(df)
A B
2017-01-01 203.0 1.175224
2017-01-02 199.0 1.338474
2017-01-04 198.0 0.652318
2017-01-05 199.0 1.577577
2017-01-07 203.0 1.732908
2017-01-08 204.0 1.473146
2017-01-09 198.0 1.109261
2017-01-10 202.0 1.745309
See the referencefor further detail.
有关更多详细信息,请参阅参考资料。
If everything is OK with your DataFrame, dropping NaNs should be as easy as that. If this is still not working, make sure you have the proper datatypes defined for your column (pd.to_numericcomes to mind...)
如果您的 DataFrame 一切正常,那么删除 NaN 应该就这么简单。如果这仍然不起作用,请确保为列定义了正确的数据类型(想到 pd.to_numeric...)
回答by Narets
----clear null all colum-------
----清除空所有列-----
df = df.dropna(how='any',axis=0)
---if you want to clean NULL by based on 1 column.---
---如果你想根据 1 列清除 NULL .---
df[~df['B'].isnull()]
A B
2017-01-01 203.0 1.175224
2017-01-02 199.0 1.338474
**2017-01-03 198.0 NaN** clean
2017-01-04 198.0 0.652318
2017-01-05 199.0 1.577577
2017-01-06 NaN 0.234882
2017-01-07 203.0 1.732908
2017-01-08 204.0 1.473146
2017-01-09 198.0 1.109261
2017-01-10 202.0 1.745309
Please forgive any mistakes.
请原谅任何错误。
回答by joe-cormier
It appears that the value in your column is "null" and not a true NaN which is what dropna is meant for. So I would try:
您的列中的值似乎是“空”,而不是真正的 NaN,而这正是 dropna 的含义。所以我会尝试:
df[df.BBD != 'null']
or, if the value is actually a NaN then,
或者,如果该值实际上是 NaN,则
df[pd.notnull(df.BBD)]
回答by Diref
You could try the following:
您可以尝试以下操作:
df.dropna(inplace=True)
df.dropna(inplace=True)
It worked for me.
它对我有用。