Jason Smith
Jason Smith

Reputation: 127

Change column values using pandas

I am using pandas and trying to replace a value with another value. What am I doing wrong?

Source

Drive-By
Referral
Website
Radio

My snippet:

import pandas as pd

second = pd.read_csv('T:/pythonfiles/result2.csv')
second['Source'] = second['Source'].replace('Drive-By', 'Drive-by')

Error:

File "pandas/src/hashtable_class_helper.pxi", line 732, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:13161)
File "pandas/src/hashtable_class_helper.pxi", line 740, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:13115)
KeyError: 'Source'

Upvotes: 2

Views: 342

Answers (1)

Fruitspunchsamurai
Fruitspunchsamurai

Reputation: 404

I believe that you can use the following as found in this answer:

second.replace({'Drive-By': 'Drive-by'}, regex=True)

This would normally replace the value in all columns, but if you don't have 'Drive-By' recurring in other columns, this should work ok.

However, your error message suggests that you have issues with 'Source' not being recognized as a key in the table.

Upvotes: 1

Related Questions