0
我有N个数据帧,范围从L1 ... Ln。如何使用Python中的循环来更改数据框熊猫?
我想修改它们来保留有关某些条件的行。
我跑到下面的循环:
for df in [L1,...,Ln]:
df=df.ix[df['Sector']=='Services']
然而,当我叫出每个数据帧,我觉得还没有相应的替代。如何使用循环修改一组数据框?
我正在使用Python 2.7。
我有N个数据帧,范围从L1 ... Ln。如何使用Python中的循环来更改数据框熊猫?
我想修改它们来保留有关某些条件的行。
我跑到下面的循环:
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']
我这不是通过DF = DF ...这样做目前? – runawaykid
@runawaykid你正在覆盖局部变量df。但不是列表中的实际DF。 – MedAli
我发现我需要在末尾添加[L1,... Ln] = all_dfs行,以便在我呼叫L1时工作。如果不是它仍然是原始数据帧。 – runawaykid