Charlie Goldberg
Charlie Goldberg

Reputation: 55

Replacing column data with Pandas .replace not working

I am attempting to turn data from a .csv into a Pandas df:

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

I want to replace the data in column "Congress" - "1(1789-1790)" - with a single date - "1789":

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

However, doing so does not change any of my data. If I, say, include inplace=True

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

... my data in that column of course becomes null. Yet I can't seem to replace this string with anything meaningful.

Upvotes: 2

Views: 5891

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476659

There are two problems here:

  1. you should use the .str part to "interpret" the column as strings; and
  2. you need to escape the brackets.
df['Congress'] = df['Congress'].str.replace(r'1\(1789-1790\)', '1789')

So we replace the string part with 1789.

Upvotes: 5

Related Questions