【发布时间】:2020-09-23 18:48:42
【问题描述】:
我是数据科学的新手,最近我一直在使用 pandas,但不知道下面这行是什么意思!
df1=df1.rename(columns=df1.iloc[0,:]).iloc[1:,:]
问题表明这是用于将索引为 11 的列作为标题,但我不明白怎么做? 我知道 rename 的使用,但无法理解多个 iloc 发生了什么?
【问题讨论】:
我是数据科学的新手,最近我一直在使用 pandas,但不知道下面这行是什么意思!
df1=df1.rename(columns=df1.iloc[0,:]).iloc[1:,:]
问题表明这是用于将索引为 11 的列作为标题,但我不明白怎么做? 我知道 rename 的使用,但无法理解多个 iloc 发生了什么?
【问题讨论】:
只需按应用的每种方法剖析线条:
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)
【讨论】: