【问题标题】:iterating through a column in dataframe and creating a list with name of the column + str遍历数据框中的列并创建一个列名 + str 的列表
【发布时间】:2018-09-05 22:31:05
【问题描述】:

我有一个 2 列的数据框,我想遍历列标题,使用该值并向其添加一个字符串并将其用作列表的名称。

rr

resampled=pd.DataFrame() resampled['RAT']=dd1['RAT'] resampled['SAT']=dd1['SAT'] rr=resampled-resampled.shift(1)

for ind, column in enumerate(rr.columns):

name=column+'stuck'
name=[]    
print(name)

我想获得 2 个名称列表 老鼠卡住了 SAT卡住了

谢谢!

【问题讨论】:

  • for 循环后的缩进?行:name=[] 您正在将变量名重新初始化为一个空列表。在代码上做一些努力。
  • 您不能命名列表。幸运的是namedtuple :)

标签: python pandas


【解决方案1】:

您尝试过列表理解吗?

[x+'stuck' for x in rr.columns]

【讨论】:

    猜你喜欢
    • 2021-07-16
    • 1970-01-01
    • 2017-10-07
    • 1970-01-01
    • 1970-01-01
    • 2023-02-04
    • 1970-01-01
    • 2020-12-13
    • 1970-01-01
    相关资源
    最近更新 更多