Friday 15 July 2011

Filtering null values from keys of dictionary- Python -



Filtering null values from keys of dictionary- Python -

i have pandas info frame , created dictionary based on columns of info frame. dictionary generated problem seek filter out nan value code doesn't work, there nan key in dictionary. code following:

for key,row in mr.iterrows(): # line seek filter out nan values doesn't work if pd.notnull(row['company namec']) , pd.notnull(row['company namea']) , pd.notnull(row['new id']) : newppmr[row['new id']]=row['company namec']

the output is:

defaultdict(<type 'list'>, {nan: '1347 property ins hldgs inc', 1.0: 'aflac inc', 2.0: 'agco corp', 3.0: 'agl resources inc', 4.0: 'invesco ltd', 5.0: 'ak steel holding corp', 6.0: 'amn healthcare services inc', nan: 'forevergreen worldwide corp'

so, don't know how filer out nan values , what's wrong code.

edit:

an illustration of pandas info frames is:

cusip company namea a�o new id company namec 42020 98912m201 nan nan nan zap 42021 989063102 nan nan nan zap.com corp 42022 98919t100 nan nan nan zaza energy corp 42023 98876r303 nan nan nan zbb energy corp

pasting illustration - how remove "nan" keys dictionary:

lets create dict 'nan' keys (nan in numeric arrays)

>>> = float("nan") >>> b = float("nan") >>> d = {a: 1, b: 2, 'c': 3} >>> d {nan: 1, nan: 2, 'c': 3}

now, lets remove 'nan' keys

>>> math import isnan >>> c = dict((k, v) k, v in d.items() if not (type(k) == float , isnan(k))) >>> c {'c': 1}

other scenario works fine. maybe i'm missing ?

in [1]: import pandas pd in [2]: import numpy np in [3]: df = pd.dataframe({'a':[1,2,3,4,np.nan],'b':[np.nan,np.nan,np.nan,5,np.nan]}) in [4]: df out[4]: b 0 1 nan 1 2 nan 2 3 nan 3 4 5 4 nan nan in [5]: key, row in df.iterrows(): print pd.notnull(row['a']) true true true true false in [6]: key, row in df.iterrows(): print pd.notnull(row['b']) false false false true false in [7]: x = {} in [8]: key, row in df.iterrows(): ....: if pd.notnull(row['b']) , pd.notnull(row['a']): ....: x[row['b']]=row['a'] ....: in [9]: x out[9]: {5.0: 4.0}

python dictionary pandas nan

No comments:

Post a Comment