Jak sprawdzić, które kolumny zawierają dowolną wartość NaN w ramce danych Pandy
Podano ramkę danych pandy zawierającą możliwe wartości NaN rozrzucone tu i ówdzie:
Pytanie: Jak określić, które kolumny zawierają wartości NaN? W szczególności, Czy Mogę uzyskać listę nazw kolumn zawierających Nan?
9 answers
UPDATE: using Pandas 0.22.0
Nowsze wersje pand mają nowe metody 'DataFrame.Isna () " i " DataFrame.notna()'
In [71]: df
Out[71]:
a b c
0 NaN 7.0 0
1 0.0 NaN 4
2 2.0 NaN 4
3 1.0 7.0 0
4 1.0 3.0 9
5 7.0 4.0 9
6 2.0 6.0 9
7 9.0 6.0 4
8 3.0 0.0 9
9 9.0 0.0 1
In [72]: df.isna().any()
Out[72]:
a True
b True
c False
dtype: bool
Jako lista kolumn:
In [74]: df.columns[df.isna().any()].tolist()
Out[74]: ['a', 'b']
Aby wybrać te kolumny (zawierające co najmniej jedną wartość NaN
):
In [73]: df.loc[:, df.isna().any()]
Out[73]:
a b
0 NaN 7.0
1 0.0 NaN
2 2.0 NaN
3 1.0 7.0
4 1.0 3.0
5 7.0 4.0
6 2.0 6.0
7 9.0 6.0
8 3.0 0.0
9 9.0 0.0
Stara odpowiedź:
Spróbuj użyć isnull():
In [97]: df
Out[97]:
a b c
0 NaN 7.0 0
1 0.0 NaN 4
2 2.0 NaN 4
3 1.0 7.0 0
4 1.0 3.0 9
5 7.0 4.0 9
6 2.0 6.0 9
7 9.0 6.0 4
8 3.0 0.0 9
9 9.0 0.0 1
In [98]: pd.isnull(df).sum() > 0
Out[98]:
a True
b True
c False
dtype: bool
Lub jako @root proponował jaśniejszą wersję:
In [5]: df.isnull().any()
Out[5]:
a True
b True
c False
dtype: bool
In [7]: df.columns[df.isnull().any()].tolist()
Out[7]: ['a', 'b']
Aby wybrać podzbiór - wszystkie kolumny zawierające co najmniej jedna NaN
wartość:
In [31]: df.loc[:, df.isnull().any()]
Out[31]:
a b
0 NaN 7.0
1 0.0 NaN
2 2.0 NaN
3 1.0 7.0
4 1.0 3.0
5 7.0 4.0
6 2.0 6.0
7 9.0 6.0
8 3.0 0.0
9 9.0 0.0
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-01-18 15:30:33
Możesz użyć df.isnull().sum()
. Pokazuje wszystkie kolumny i całkowite Nan każdej funkcji.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-01-18 14:33:14
Miałem problem, gdzie musiałem wiele kolumn do wizualnej kontroli na ekranie tak krótki Comp listy, że filtruje i zwraca obrażające kolumny jest
nan_cols = [i for i in df.columns if df[i].isnull().any()]
If that ' s helpful to anyone
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-08-07 07:25:09
W zestawach danych o dużej liczbie kolumn jeszcze lepiej jest sprawdzić, ile kolumn zawiera wartości null, a ile nie.
print("No. of columns containing null values")
print(len(df.columns[df.isna().any()]))
print("No. of columns not containing null values")
print(len(df.columns[df.notna().all()]))
print("Total no. of columns in the dataframe")
print(len(df.columns))
Na przykład w moim dataframe zawierała ona 82 kolumny, z czego 19 zawierało co najmniej jedną wartość null.
Ponadto możesz również automatycznie usunąć cols i rows w zależności od tego, który ma więcej wartości null
Oto kod, który robi to inteligentnie:
df = df.drop(df.columns[df.isna().sum()>len(df.columns)],axis = 1)
df = df.dropna(axis = 0).reset_index(drop=True)
Uwaga: powyższy kod usuwa wszystkie Twoje null wartości. Jeśli chcesz wartości null, przetworz je wcześniej.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-02-17 10:57:26
To zadziałało dla mnie,
1. Do uzyskania kolumn o co najmniej 1 wartości null. (nazwy kolumn)
data.columns[data.isnull().any()]
2. Do uzyskania kolumn z count, z co najmniej 1 wartością null.
data[data.columns[data.isnull().any()]].isnull().sum()
[opcjonalne] 3. Aby uzyskać procent liczby null.
data[data.columns[data.isnull().any()]].isnull().sum() * 100 / data.shape[0]
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-06-17 16:25:35
Używam tych trzech linii kodu, aby wydrukować nazwy kolumn, które zawierają co najmniej jedną wartość null:
for column in dataframe:
if dataframe[column].isnull().any():
print('{0} has {1} null values'.format(column, dataframe[column].isnull().sum()))
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-12-07 17:05:53
Oba powinny działać:
df.isnull().sum()
df.isna().sum()
Metody ramek danych isna()
lub isnull()
są całkowicie identyczne.
Uwaga : puste ciągi {[3] } są uważane za False (nie są brane pod uwagę NA)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-05-06 22:00:54
df.columns[df.isnull().any()].tolist()
Zwróci nazwę kolumn zawierających wiersze null
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2021-01-09 02:03:06
df.isna()
zwraca True wartości dla NaN, False dla reszty. Tak, robi:
df.isna().any()
Zwróci True dla każdej kolumny mającej NaN, False dla reszty
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-11-11 23:12:03