[英]Avoid overwriting dataset in Python forloop
price
date price fruit
2010-01-04 0.83 banana
2010-01-04 0.05 apple
對於每個水果,如果那個水果 ==True,你怎么能保留,然后在處理那個特定水果時暫時放下水果列?
listxx = [(price, "price")]
fruits = ['apple', 'banana', 'pear']
for fruit in fruits:
for x, y in listxx:
x[x['fruit'] == fruit]
x.drop(['fruit'], axis=1, inplace=True)
目前,當我到達香蕉時,由於蘋果,水果列已經被刪除。
迭代香蕉時,價格數據集應如下所示:
date price
2010-01-04 0.83
迭代蘋果時,價格數據集應該是:
date price
2010-01-04 0.05
我需要價格數據集來臨時刪除固定列並保留如果水果 = 那個水果。 然后 go 回到原始數據集為下一個水果做同樣的事情。
實際上,這意味着使用過濾后的數據創建一個新數據集。 我們將給它一個單獨的名稱,以便我們可以 i) 實際上引用檢查行的結果,並且 ii) 從該結果中刪除列,而不是原始結果。
我們還將努力以一種易於理解的方式命名事物。
tables_and_names = [(price, "price")]
fruits = ['apple', 'banana', 'pear']
for fruit in fruits:
for table, name in tables_and_names:
filtered_table = table[table['fruit'] == fruit]
filtered_table.drop(['fruit'], axis=1, inplace=True)
# now we can do more logic with the filtered_table
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.