从pandas DataFrame删除包含空单元格的行 [英] Drop rows containing empty cells from a pandas DataFrame
问题描述
我有一个pd.DataFrame
,它是通过解析一些excel电子表格创建的.一列的单元格为空.例如,下面是该列的频率输出,有32320条记录缺少 Tenant 的值.
I have a pd.DataFrame
that was created by parsing some excel spreadsheets. A column of which has empty cells. For example, below is the output for the frequency of that column, 32320 records have missing values for Tenant.
>>> value_counts(Tenant, normalize=False)
32320
Thunderhead 8170
Big Data Others 5700
Cloud Cruiser 5700
Partnerpedia 5700
Comcast 5700
SDP 5700
Agora 5700
dtype: int64
我正在尝试删除缺少租户的行,但是.isnull()
选项无法识别丢失的值.
I am trying to drop rows where Tenant is missing, however .isnull()
option does not recognize the missing values.
>>> df['Tenant'].isnull().sum()
0
该列的数据类型为对象".在这种情况下会发生什么?如何在缺少 Tenant 的地方删除记录?
The column has data type "Object". What is happening in this case? How can I drop records where Tenant is missing?
推荐答案
如果熊猫是np.nan
对象,则它将识别为空值,该对象将在DataFrame中打印为NaN
.您缺少的值可能是空字符串,Pandas不能将其识别为null.要解决此问题,您可以使用replace()
将空字符串(或空单元格中的任何东西)转换为np.nan
对象,然后在DataFrame上调用dropna()
删除具有空租户的行.
Pandas will recognise a value as null if it is a np.nan
object, which will print as NaN
in the DataFrame. Your missing values are probably empty strings, which Pandas doesn't recognise as null. To fix this, you can convert the empty stings (or whatever is in your empty cells) to np.nan
objects using replace()
, and then call dropna()
on your DataFrame to delete rows with null tenants.
为了演示,我们在Tenants
列中创建一个带有一些随机值和一些空字符串的DataFrame:
To demonstrate, we create a DataFrame with some random values and some empty strings in a Tenants
column:
>>> import pandas as pd
>>> import numpy as np
>>>
>>> df = pd.DataFrame(np.random.randn(10, 2), columns=list('AB'))
>>> df['Tenant'] = np.random.choice(['Babar', 'Rataxes', ''], 10)
>>> print df
A B Tenant
0 -0.588412 -1.179306 Babar
1 -0.008562 0.725239
2 0.282146 0.421721 Rataxes
3 0.627611 -0.661126 Babar
4 0.805304 -0.834214
5 -0.514568 1.890647 Babar
6 -1.188436 0.294792 Rataxes
7 1.471766 -0.267807 Babar
8 -1.730745 1.358165 Rataxes
9 0.066946 0.375640
现在,我们将Tenants
列中的所有空字符串替换为np.nan
对象,如下所示:
Now we replace any empty strings in the Tenants
column with np.nan
objects, like so:
>>> df['Tenant'].replace('', np.nan, inplace=True)
>>> print df
A B Tenant
0 -0.588412 -1.179306 Babar
1 -0.008562 0.725239 NaN
2 0.282146 0.421721 Rataxes
3 0.627611 -0.661126 Babar
4 0.805304 -0.834214 NaN
5 -0.514568 1.890647 Babar
6 -1.188436 0.294792 Rataxes
7 1.471766 -0.267807 Babar
8 -1.730745 1.358165 Rataxes
9 0.066946 0.375640 NaN
现在我们可以删除空值:
Now we can drop the null values:
>>> df.dropna(subset=['Tenant'], inplace=True)
>>> print df
A B Tenant
0 -0.588412 -1.179306 Babar
2 0.282146 0.421721 Rataxes
3 0.627611 -0.661126 Babar
5 -0.514568 1.890647 Babar
6 -1.188436 0.294792 Rataxes
7 1.471766 -0.267807 Babar
8 -1.730745 1.358165 Rataxes
这篇关于从pandas DataFrame删除包含空单元格的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!