2017-07-31 21 views
0

我有N個數據幀,範圍從L1 ... Ln。如何使用Python中的循環來更改數據框熊貓?

我想修改它們來保留有關某些條件的行。

我跑到下面的循環:

for df in [L1,...,Ln]: 
    df=df.ix[df['Sector']=='Services'] 

然而,當我叫出每個數據幀,我覺得還沒有相應的替代。如何使用循環修改一組數據框?

我正在使用Python 2.7。

回答

1

你需要用新的覆蓋舊數據框:

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'] 
+0

我這不是通過DF = DF ...這樣做目前? – runawaykid

+1

@runawaykid你正在覆蓋局部變量df。但不是列表中的實際DF。 – MedAli

+0

我發現我需要在末尾添加[L1,... Ln] = all_dfs行,以便在我呼叫L1時工作。如果不是它仍然是原始數據幀。 – runawaykid

相關問題