简体   繁体   中英

With `.to_dict()`, pandas converts `NaN` to `nan` in python dictionary ; How to check if 'nan' exists in python

Here is a sample CSV file:

X,Y
A,
B,
C,D

After reading this file, pandas treats empty cell as NaN values:

df = pd.read_csv("test.csv")
  X   Y
0 A NaN
1 B NaN
2 C   D

and after converting this to lists of python dictionaries, NaN becomes nan

>> d = df.to_dict(orient='records')

[{'X': 'A', 'Y': nan}, {'X': 'B', 'Y': nan}, {'X': 'C', 'Y': 'D'}]

I'm trying to find where null exits using math.isnan() , but it throws an exception

for i,v in enumerate(d):
    if math.isnan(v['Y']):
        print(I)
0
1
TypeError: must be real number, not str

Exception can be handle using

for i,v in enumerate(d):
    try:
        if math.isnan(v['Y']):
            print(i)
    except:
        pass

But is there a better way to find nan values??

IIUC use pandas.isna :

for i,v in enumerate(d):
    if pd.isna(v['Y']):
        print('I')

You don't need any function, you can compare the value to itself. NaNs have this interesting property to be different from themselves:

for i,v in enumerate(d):
    if v['Y']!=v['Y']:
        print(i)

output

0
1

Another possibility would be to generate this list from the DataFrame itself (note that you would get the real indices here, so 0, 1 if this is a range index otherwise the first and second values of the index):

s = df['Y'].isna()
na_indices = s[s].index.to_list()

output: [0, 1]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM