【问题标题】:Intepreting Pandas Column Referencing Syntax解释 Pandas 列引用语法
【发布时间】:2020-04-14 00:00:33
【问题描述】:

我有使用 R 进行数据处理的基本背景,但我是 Python 新手。我从 Coursera 上的教程中发现了这段代码 sn-p。

有人可以向我解释一下 columns ={col:'Gold' + col[4:]}, inplace = True 是什么意思吗?

(1)据我了解,df.rename是将已有的列名重命名为(第一行的情况下为Gold),但是为什么后面还要加上+col[4:]呢?

(2) 声明函数inplace为True是否意味着将得到的df输出分配给原始df?

import pandas as pd

df = pd.read_csv('olympics.csv', index_col=0, skiprows=1)

for col in df.columns:
    if col[:2]=='01':
        df.rename(columns={col:'Gold'+col[4:]}, inplace=True)
    if col[:2]=='02':
        df.rename(columns={col:'Silver'+col[4:]}, inplace=True)
    if col[:2]=='03':
        df.rename(columns={col:'Bronze'+col[4:]}, inplace=True)
    if col[:1]=='№':
        df.rename(columns={col:'#'+col[1:]}, inplace=True)

提前谢谢你。

【问题讨论】:

  • 似乎 'Gold'+col[4:] 将是新名称。指定原始列名将有助于回答您的问题。
  • @rohit-biswas 原来的列名是 01 ! 在它被替换为“Gold”之前

标签: python pandas


【解决方案1】:

意思是:

#for each column name
for col in df.columns:
    #check first 2 chars for 01
    if col[:2]=='01':
        #replace column name with text gold and all characters after 4th letter
        df.rename(columns={col:'Gold'+col[4:]}, inplace=True)
    #similar like above
    if col[:2]=='02':
        df.rename(columns={col:'Silver'+col[4:]}, inplace=True)
    #similar like above
    if col[:2]=='03':
        df.rename(columns={col:'Bronze'+col[4:]}, inplace=True)
    #check first letter
    if col[:1]=='№':
        #add # after first letter
        df.rename(columns={col:'#'+col[1:]}, inplace=True)

将函数就地声明为 True 是否意味着将结果 df 输出分配给原始数据帧

是的,你是对的。它替换就地列名。

【讨论】:

  • @Rosellx - 现在明白了吗?还是有必要解释更多?
  • 是的,现在了解inplace的目的,但是如果我在重命名数据框的列时不声明就地,这意味着什么?
  • @Rosellx - 然后使用 df = df.rename(columns={col:'Silver'+col[4:]}) - 只需将值重新赋值
  • 这意味着 df = df.rename(columns={col:'Silver'+col[4:]}) 将只是对列的临时重命名?换句话说,inplace=False 会保留数据框的原始列名,对吧?
  • @Rosellx - 它可以替代没有inplace的代码
【解决方案2】:
if col[:2]=='01':
        #replace column name with text gold and all characters after 4th letter
        df.rename(columns={col:'Gold'+col[4:]}, inplace=True)

(1)。如果 col 的列名是“01xx1234”,
1. col[:2] = 01 为真
2. 'Gold'+col[4:] => 'Gold'+col[4:] => 'Gold1234'
3. 所以,'01xx1234' 被'Gold1234' 取代。

(2) inplace = True 直接应用于数据帧,不返回结果。
如果不加这个选项,就必须这样做。
df = df.rename(columns={col:'Gold'+col[4:]})

【讨论】:

    【解决方案3】:

    inplace=True 表示:这些列将在您的原始数据框 (df) 中重命名

    您的情况(inplace=True):

    import pandas as pd
    
    df = pd.DataFrame(columns={"A": [1, 2, 3], "B": [4, 5, 6]})
    df.rename(columns={"A": "a", "B": "c"}, inplace=True)
    
    print(df.columns)
    # Index(['a', 'c'], dtype='object')
    # df already has the renamed columns, because inplace=True.
    

    如果您不使用 inplace=True,那么 rename 方法将生成一个新的数据框,如下所示:

    import pandas as pd
    df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
    new_frame = df.rename(columns={"A": "a", "B": "c"})
    
    print(df.columns) 
    # Index(['A', 'B'], dtype='object') 
    # It contains the old column names
    
    print(new_frame.columns)
    # Index(['a', 'c'], dtype='object') 
    # It's a new dataframe and has renamed columns
    

    注意:在这种情况下,将新数据帧分配给原始数据帧 (df) 的更好方法

    df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
    df = df.rename(columns={"A": "a", "B": "c"})
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-13
      • 2015-08-21
      相关资源
      最近更新 更多