sdasdadas sdasdadas -4 years ago 89
Python Question

Updating columns in DataFrame using a Series

I have a

object in the pandas with multiple rows and columns. For illustration purposes, assume I have two such columns named Address and Age.

I also have a
object that maps a number of addresses to ages.

Now, I would like to update the dataframe using that series. Here's how I currently do it:

for (address, age) in series.iteritems():
df[df.address == address].age = age

This seems to work but it's too slow. It takes over a minute to update all the entries.

Is there a faster way to do this update (preferably without using a for loop)?

Answer Source

I think you need map:

df = pd.DataFrame({'Address':['a','d','f'],

print (df)
  Address  Age
0       a    4
1       d    5
2       f    6

s = pd.Series([10,4,6], index=['a','f','d'])
print (s)
a    10
f     4
d     6
dtype: int64

df.Age =
#a bit faster in big df
#df.Age =
print (df)
  Address  Age
0       a   10
1       d    6
2       f    4
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download