Reputation: 1956
I have a DataFrame which can contain columns with the same column name. Based on the value I want to rename the column name so there are no duplicates. I've tried a few things, but every time I try to iterate over the columns and rename them I end up with the column name. df.rename(columns=df.columns[i]: 'some_name'}) seems to use the column name as well.
Let's say I have a dataframe;
df = pd.DataFrame({"A": [10kg], "B": [4], "A": [4%]})
I would like to rename the column(s) named "A" based on the row value so that I get
A B A%
0 10kg 4 4
I tried something like this:
for i in range(0, len(df.columns)):
if 'A' in df.columns[i]:
if '%' in df.iloc[:,i].values[0]:
df = df.rename(columns={df.columns[i]: 'A_%'})
But this also renames the first column 'A'. Is there another way to rename it based on location?
Upvotes: 1
Views: 1763
Reputation: 6642
Single list comprehension for new column names:
import pandas as pd
df = pd.concat([pd.DataFrame({"A": ['10kg'], "B": ['4']}),
pd.DataFrame({"A": ['4%']})], axis=1)
df.columns = [c + '_%'
if df.applymap(lambda x: '%' in x).any(axis=0).iloc[ic]
else c for ic, c in enumerate(df.columns)]
Edit -- better:
import pandas as pd
df = pd.concat([pd.DataFrame({"A": ['10kg'], "B": ['4']}),
pd.DataFrame({"A": ['4%']})], axis=1)
has_percentage = df.applymap(lambda x: '%' in x).any(axis=0)
df.columns = [c + '_%' if has_percentage.iloc[ic]
else c for ic, c in enumerate(df.columns)]
Upvotes: 3
Reputation: 364
You could create a list with all the column names, change the i'th column name in that list and use that list to redefine the column names:
for i in range(0, len(df.columns)):
if 'A' in df.columns[i]:
if '%' in df.iloc[:,i].values[0]:
columnnames = list(df.columns)
columnnames[i] = 'A_%'
df.columns = columnnames
Upvotes: 1