避免在 Python forloop 中覆盖数据集

数字
price
date            price      fruit
2010-01-04    0.83        banana
2010-01-04    0.05         apple

对于每个水果,如果那个水果==真,你如何保持,然后在处理那个特定水果时暂时放下水果列?

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       
卡尔·克内克特尔

我需要价格数据集来临时删除公司列并保留如果水果 = 那个水果。然后回到下一个水果的原始数据集做同样的事情。

实际上,这意味着使用过滤后的数据创建一个数据集。我们将给它一个单独的名称,以便我们可以 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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章