【发布时间】:2020-12-22 01:25:23
【问题描述】:
DataFrame 1 - 按日期划分的水果价格(索引是日期)
fruits_price = {'Apple': [9,5,14],
'Orange': [10,12,10],
'Kiwi': [5,4,20],
'Watermelon': [4.4,5.4,6.4]}
df1 = pd.DataFrame(fruits_price,
columns = ['Apple','Orange','Kiwi','Watermelon'],
index=['2020-01-01','2020-01-02','2020-01-10'])
date Apple Oranges Kiwi Watermelon ... Fruit_100
2020-01-01 9 10 5 4.4
2002-01-02 5 12 4 5.4
...
2002-12-10 14 10 20 6.4
Dataframe 2(按排名排列的顶级水果)(索引是日期)
top_fruits = {'Fruit_1': ['Apple','Apple','Apple'],
'Fruit_2': ['Kiwi','Orange','Kiwi'],
'Fruit_3': ['Orange','Watermelon','Watermelon'],
'Fruit_4': ['Watermelon','Kiwi','Orange']}
df2 = pd.DataFrame(top_fruits,
columns = ['Fruit_1','Fruit_2','Fruit_3','Fruit_4'],
index=['2020-01-01','2020-01-02','2020-01-10'])
date Fruit_1 Fruit_2 Fruit_3 Fruit_4 ... Fruit_100
2020-01-01 Apple Kiwi Oranges Watermelon Pineapple
2002-01-02 Apple Oranges Watermelon Kiwi Pineapple
...
2002-12-10 Apple Kiwi Watermelon Oranges Pineapple
我想要 DataFrame 3(给定日期的顶级水果的价格) 它实际上告诉我在给定日期的顶级水果的价格
date Price_1 Price_2 Price_3 Price_4 ..... Price_100
2020-01-01 9 5 10 4.4
2002-01-02 5 12 5.4 4
...
2002-12-10 14 20 6.4 10
花了将近 1 晚,尝试迭代 Dataframe 2,然后在 DataFrame 1 上进行内部循环,并向 DataFrame 3 添加值。我已经尝试了几乎 6-7 种不同的方式,通过 iterrow ,iteritems,然后通过 iloc 直接将输出存储到df3。这些都不起作用。
只是想知道有一种更简单的方法可以做到这一点。 稍后我将乘以相同数据帧格式中的水果销售额。
【问题讨论】:
-
Does this help?(
lookup()上的 W3 文章)。而不是self使用其他df -
对不起,“使用其他数据框”是什么意思? @诺亚
标签: python pandas dataframe merge