Charlie Goldberg
Charlie Goldberg

Reputation: 55

pandas .replace removes first value but doesn't add second

I am attempting to replace data in a .csv column: '1(1789-1790)' with a single year: '1789'

I first read the .csv:

df = pd.read_csv('congress1.csv', delimiter = ';', names = ['Name', 'Years', 'Position', 'Party', 'State', 'Congress'], header = 0)

Then I run .replace on data in column='Congress' and provide it with the new string:

df['Congress'] = df['Congress'].replace('1(1789-1790)', '1789', inplace=True)

However, when I print my data that column shows "None."

Upvotes: 1

Views: 34

Answers (1)

timgeb
timgeb

Reputation: 78750

When you use inplace=True the replace method returns None (which you then reassign to) and modifies the object in-place.

Demo:

>>> s = pd.Series([1,2,3])
>>> s
>>> 
0    1
1    2
2    3
dtype: int64
>>> 
>>> res = s.replace(1, 2, inplace=True)
>>> res is None
>>> True
>>> s
>>> 
0    2
1    2
2    3
dtype: int64

s has been modified in-place, res is None!

You can simply omit the inplace=True part or don't reassign to df['Congress'].

Upvotes: 1

Related Questions