sdasdadas
sdasdadas

Reputation: 25096

Updating columns in DataFrame using a Series

I have a DataFrame 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 Series 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)?

Upvotes: 2

Views: 417

Answers (1)

jezrael
jezrael

Reputation: 862671

I think you need map:

df = pd.DataFrame({'Address':['a','d','f'],
                   'Age':[4,5,6]})

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 = df.Address.map(s)
#a bit faster in big df
#df.Age = df.Address.map(s.to_dict())
print (df)
  Address  Age
0       a   10
1       d    6
2       f    4

Upvotes: 2

Related Questions