Reputation: 55
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
Reputation: 476659
There are two problems here:
.str
part to "interpret" the column as strings; anddf['Congress'] = df['Congress'].str.replace(r'1\(1789-1790\)', '1789')
So we replace the string part with 1789
.
Upvotes: 5