简体   繁体   English

根据 DataFrame 中的值重命名具有相同列名的列

[英]Rename column with same column name based on values in DataFrame

I have a DataFrame which can contain columns with the same column name.我有一个 DataFrame 可以包含具有相同列名的列。 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. df.rename(columns=df.columns[i]: 'some_name'}) 似乎也使用了列名。

Let's say I have a dataframe;假设我有一个 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”的列,以便得到

   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'.但这也将第一列重命名为“A”。 Is there another way to rename it based on location?还有其他方法可以根据位置重命名吗?

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)]

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:您可以创建一个包含所有列名的列表,更改该列表中的第 i 个列名并使用该列表重新定义列名:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM