【发布时间】:2021-01-02 13:20:49
【问题描述】:
所以我有一个 DataFrame 列表:
list_table = [a, b, c, d, e, f, g]
我想从每个 DataFrame 中删除 "Unnamed: 36" 列并将数据类型更改为数字,并且我还想创建一个新列,该列从名为 'Total' 的每一行的总和中获得。
这是我的to_numeric 函数:
def to_numeric(df):
col = df.columns
for i in range(len(col)):
df[col[i]] = pd.to_numeric(
df[col[i]].fillna(0).apply(
lambda x: str(x).replace(",", "")
)
)
return df
我的 for 循环进行处理:
for newtable in list_table:
newtable = newtable.drop("Unnamed: 36", axis=1)
newtable = to_numeric(newtable)
newtable['Total'] = newtable.sum(axis=1)
newtable.index = pd.to_datetime(newtable.index)
但是在处理循环之后,每个 DataFrame 都没有改变,所以我有点困惑这样做。谁能帮我解决这个问题?
【问题讨论】:
-
这些方法可能不会就地进行更改 - 请参阅文档https://pandas.pydata.org/docs/user_guide/index.html
-
当您使用变量
a或d时,您希望看到这些变化吗?
标签: python dataframe jupyter-notebook