Correct Use Of Map For Mapping A Function Onto A Df, Python Pandas
Searching for awhile now and can't get anything concrete on this. Looking for a best practice answer. My code works, but I'm not sure if I'm introducing problems. # df['Action']
Solution 1:
So first question, which of these is better practice and what exactly is the first one doing?
df['Action'] = df['param1'].map(my_function)
is much more idiomatic, faster (vectorized) and more reliable.
2nd and final question. This is the more important of the two. Map, apply, applymap - not sure which to use here. The first commented out line of code does NOT work, while the second gives me exactly what I want.
Pandas does NOT have DataFrame.map()
- only Series.map()
, so if you need to access multiple columns in your mapping function - you can use DataFrame.apply()
.
Demo:
df['New_Col'] = df.apply(lamba x: my_function(x.param1,
x.param1.shift(1),
x.param2.shift(1),
axis=1)
or just:
df['New_Col'] = my_function(df.param1, df.param1.shift(1), df.param2.shift(1))
Post a Comment for "Correct Use Of Map For Mapping A Function Onto A Df, Python Pandas"