【发布时间】:2018-01-06 20:20:19
【问题描述】:
我有 N 个数据帧,范围从 L1...Ln。
我想修改它们以保持与特定条件相关的行。
我运行了以下循环:
for df in [L1,...,Ln]:
df=df.ix[df['Sector']=='Services']
但是,当我调出每个数据框时,我发现它并没有被相应地替换。如何使用循环修改一组数据帧?
我使用的是 Python 2.7。
【问题讨论】:
我有 N 个数据帧,范围从 L1...Ln。
我想修改它们以保持与特定条件相关的行。
我运行了以下循环:
for df in [L1,...,Ln]:
df=df.ix[df['Sector']=='Services']
但是,当我调出每个数据框时,我发现它并没有被相应地替换。如何使用循环修改一组数据帧?
我使用的是 Python 2.7。
【问题讨论】:
你需要用新的数据框覆盖旧的数据框:
all_dfs = [L1,...,Ln]
# iterate through the dataframes one by one
# keep track of the order in index and the content in df
for index, df in enumerate(all_dfs):
# modify the current dataframe df
# then overwrite the old one in the same index.
all_dfs[index]= df.ix[df['Sector']=='Services']
【讨论】: