【问题标题】:how to modify dataframe when referring them through a list loop [duplicate]通过列表循环引用它们时如何修改数据框[重复]
【发布时间】:2022-01-08 15:06:55
【问题描述】:

我有很多数据框,我将它们存储在一个列表中。

现在我想对每个数据帧做简单的fillna(0),所以我做了以下,但没有奏效:

df_list = [df_1,df_2,df_3,df_4]

for df in df_list:
    df = df.fillna(0)
    df.index = df.index.strftime('%Y-%m-%d')

我认为循环内左侧的df与原始数据框不一样,怎么办?

【问题讨论】:

标签: python


【解决方案1】:

在循环的第一行中,您定义了一个新的数据框,并且什么也不做。

相反,您可以只使用 inplace = True 对数据框进行处理,而无需创建新的。

for df in df_list:
    df.fillna(0, inplace = True)
    df.index=df.index.strftime('%Y-%m-%d')

【讨论】:

  • 谢谢百事可乐
  • 谢谢 Pepsi-joe,如果我们像这样使用字典,for (key,df) in df_dict.items(): # 不只是项目,使用 iterms() df_dict[key]=df. fillna(0),为什么原来的df没有变化,而是用df_dict[key] change返回了df?
猜你喜欢
  • 2020-05-22
  • 2018-10-18
  • 2019-03-21
  • 2021-01-19
  • 2012-07-25
  • 2018-12-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-20
相关资源
最近更新 更多