【问题标题】:Iloc and rename in pandas大熊猫中的 Iloc 和重命名
【发布时间】:2020-09-23 18:48:42
【问题描述】:

我是数据科学的新手,最近我一直在使用 pandas,但不知道下面这行是什么意思!

df1=df1.rename(columns=df1.iloc[0,:]).iloc[1:,:]

问题表明这是用于将索引为 11 的列作为标题,但我不明白怎么做? 我知道 rename 的使用,但无法理解多个 iloc 发生了什么?

【问题讨论】:

    标签: python pandas rename


    【解决方案1】:

    只需按应用的每种方法剖析线条:

    df1 =                     # reassign df1 to ...
       df1.rename(            # the renamed frame of df1 ...
           columns =          # where column names will use mapper of ...
               df1.iloc[0,:]  # slice of df1 on row 0, include all columns ...
       )
       .iloc[1:,:]            # the slice of the renamed frame from row 1 forward, include all columns...
    

    实际上,它删除了第一行并设置为列名,可以类似地完成:

    df1.columns = df1.iloc[0, :]
    df1.drop(0, inplace=True)
    

    【讨论】:

      猜你喜欢
      • 2020-07-09
      • 2021-12-06
      • 1970-01-01
      • 2021-08-15
      • 2018-03-07
      • 2019-05-25
      • 2017-01-23
      • 2019-07-28
      相关资源
      最近更新 更多