dataFrame列中的空Na

时间:2018-11-18 01:57:04

标签: python dataframe

df

  Letter    city    state
0   A        NYC    NY
1   B        Na     CT
2   C        LA     Na
3   D       Tampa   FL
4   E        Na     Na
5   F       Dallas  TX
6   G       Denver  CL

df['city']=df['city'].str.replace("Na"," ")
df['state']=df['state'].str.replace("Na"," ")

df

    Letter  city    state
0     A     NYC     NY
1     B     CT
2     C     LA  
3     D   Tampa     FL
4     E     
5     F   Dallas    TX
6     G   Denver    CL

df.isnull().any()
Letter    False
city      False
state     False
dtype: bool

如何清空Na成为:

Letter  False
city    True
state   True

1 个答案:

答案 0 :(得分:2)

从原始的df开始,您可以这样做:

df.eq("Na").any()

或者,从第二个df开始,用空字符串替换Na之后,用NaN替换空字符串:

import numpy as np

df.replace('', np.nan).isnull().any()

两种产品:

Letter    False
city       True
state      True
dtype: bool