I'm trying to understand my mistake when using df.rename in pandas. Specifically, using the rename function with a tuple executes without error, but no changes are made to the column names.
f_GreaterArea = pd.DataFrame(np.random.randn(5, 3),
index=['a', 'c', 'e', 'f', 'h'],
columns=['one', 'two', 'three'])
print(f_GreaterArea)
one two three
a 0.278969 -0.676388 -2.464444
c -0.992077 -0.435534 2.267315
e 2.094669 -1.401885 1.243658
f 0.886835 0.195726 -0.132382
h -0.920486 -0.298380 2.227378
old_colnames = ('one', 'two', 'three')
new_colnames = ('pig', 'cups', 'seven')
f_GreaterArea.rename(columns={old_colnames:new_colnames}, inplace=True)
print(f_GreaterArea)
one two three
a 0.278969 -0.676388 -2.464444
c -0.992077 -0.435534 2.267315
e 2.094669 -1.401885 1.243658
f 0.886835 0.195726 -0.132382
h -0.920486 -0.298380 2.227378
In order to replace a value in Pandas DataFrame, use the replace() method with the column the from and to values. Below example replace Spark with PySpark value on the Course column. Notice that all the Spark values are replaced with the Pyspark values under the first column.
To replace multiple values in a DataFrame, you can use DataFrame. replace() method with a dictionary of different replacements passed as argument.
columns parameter should be this way:
{'one': 'pig', 'three': 'seven', 'two': 'cups'}
use this code to get it:
dict(zip(old_colnames, new_colnames))
If you want to change name of columns, use this code will be more easy:
f_GreaterArea.columns = ('pig', 'cups', 'seven')
You are correct in wanting to pass in a dict
with three entries, one for each column you are renaming, but the dict
you are passing is not. It's a dict
of one entry, with one tuple as a key and one as a value.
Use a dict
comprehension to turn the tuples into a dict
, like this:
{i:j for i,j in zip(old_colnames,new_colnames)}
So in the context of your code, that's:
col_rename_dict = {i:j for i,j in zip(old_colnames,new_colnames)}
f_GreaterArea.rename(columns=col_rename_dict, inplace=True)
Or just:
f_GreaterArea.rename(
columns={i:j for i,j in zip(old_colnames,new_colnames)}, inplace=True
)
Here's a nice little write-up on comprehensions in general, including the dict
comprehension. It also includes usage of zip
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With