Python 从数据帧中删除具有空值的行
我试图从数据框中删除一行,其中一列的值为null。我能找到的大部分帮助都与删除NaN值有关,到目前为止,NaN值对我不起作用 在这里,我创建了数据框:Python 从数据帧中删除具有空值的行,python,pandas,dataframe,null,Python,Pandas,Dataframe,Null,我试图从数据框中删除一行,其中一列的值为null。我能找到的大部分帮助都与删除NaN值有关,到目前为止,NaN值对我不起作用 在这里,我创建了数据框: # 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 o
# 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)
以下是我的输出:
有人能告诉我如何删除这一行吗,最好是通过空值标识该行,以及如何按日期删除
我和熊猫合作的时间不长,我已经在这上面呆了一个小时了。任何建议都将不胜感激 这应该可以完成以下工作:
df = df.dropna(how='any',axis=0)
它将删除其中包含“any”空值的每一行(axis=0)
示例:
#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
有关更多详细信息,请参阅
如果您的数据帧一切正常,那么删除NAN应该很容易。如果这仍然不起作用,请确保为列定义了正确的数据类型(想到…)列中的值似乎为“null”,而不是真正的NaN,这正是dropna的含义。因此,我会尝试:
df[df.BBD != 'null']
或者,如果该值实际上是NaN
df[pd.notnull(df.BBD)]
----清除所有列的空值-------
---如果要根据1列清除空值---
请原谅任何错误。删除所有空值dropna()方法将非常有用
df.dropna(inplace=True)
要删除包含空值的特定值,请使用此代码
df.dropna(subset=['column_name_to_remove'], inplace=True)
我建议尝试以下两行中的一行:
df_clean = df1[df1['BBD'].isnull() == False]
df_clean = df1[df1['BBD'].isna() == False]
我的解决方法是在参数na_values(['NaN','null'])中包含'null',get将其传递给pandas.read_csv()以创建df。这对我来说很好,谢谢你。也适用于提取唯一的非空值。.df[~df['B'].isnull()].unique()
df.dropna(inplace=True)
df.dropna(subset=['column_name_to_remove'], inplace=True)
df_clean = df1[df1['BBD'].isnull() == False]
df_clean = df1[df1['BBD'].isna() == False]