[英]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.