I want to apply a mapping to a Pandas DataFrame, where some rows in one specific column are NaN. These are left overs from a prior mapping.
mymap = defaultdict(str)
mymap["a"] = "-1 test"
mymap["b"] = "-2 test"
mymap["c"] = "-3 test"
df[ df["my_infos"].isnull() ] = df["something"].map(lambda ip: map_function(ip, mymap))
Here is the function:
def map_function(ip, mymap):
# do stuff
for key, value in mymap.iteritems():
# do stuff
return stuff
return other_stuff
This terminates after it iterates over the whole column, but shows:
File "/opt/anaconda2/lib/python2.7/site-packages/pandas/core/indexing.py", line 527, in _setitem_with_indexer raise ValueError('Must have equal len keys and value ' ValueError: Must have equal len keys and value when setting with an iterable
Is this the correct way to select all rows in the my_infos column, which are NaN? I somehow sense... that's not the case.
I think you can select NaN values to both sides and then map:
df[ df["my_infos"].isnull() ] =
df.ix[ df["my_infos"].isnull(), "something"].map(lambda ip: map_function(ip, mymap))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With