Python : How to rename the column name that changes with the unique value in other column?

mathew :
data = {'pop': [2.0, 3.0, 4.0],'county':['jpy','jpy','jpy']}
df = pd.DataFrame(data)

I want to change the column('pop') name dynamically with the unique value in other column. I do not want to hard code the column value. I'm trying something like below but it's throwing an error.

 value = df.county.unique()
 df.rename(columns={'pop': str(value)}, inplace=True)

Expected output:

df = jpy    county
    2.0     jpy
    3.0     jpy
    4.0     jpy
kederrac :

with the given data you can use:

df.rename(columns={'pop': str(*value)}, inplace=True)

you will cast to str the only element from value

output:

enter image description here

also you can use:

df.rename(columns={'pop': list(value)[0]}, inplace=True)

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=386990&siteId=1