python代码基于另一个dataFrame返回数据框中的元素值
我也有一个类似于使用年度数据生成的文件类似的数据集
d1 = pd.DataFrame({'category': ['A', 'B', 'C', 'D', 'E', 'F'],
'col': ['20%', '40%', '80%', '40%', '60%', '20%']})
,类似于此的数据集是使用另一个数据集生成的数据集,该数据集
d2 = pd.DataFrame({'category': ['A', 'B', 'C', 'D', 'E', 'F'],
'20%': ['2.1', '4.1', '6.8', '5.9', '3.4', '5.4'],
'40%': ['1.9', '3.7', '6.1', '4.8', '4.1', '7.2'],
'60%': ['3.1', '4.9', '6.5', '7.1', '7.9', '5.1'],
'80%': ['2.5', '4.5', '5.6', '6.9', '8.4', '7.4']})
我希望获得这样的输出,在其中它将基于D2返回元素的值。 D1中的两个列
op = pd.DataFrame({'category': ['A', 'B', 'C', 'D', 'E', 'F'],
'col': ['20%', '40%', '80%', '40%', '60%', '20%'],
'min_value': ['2.1', '3.7', '5.6', '4.8', '7.9', '5.4']})
我不确定如何看待值
I have a dataset similar to this generated from a file with yearly data
d1 = pd.DataFrame({'category': ['A', 'B', 'C', 'D', 'E', 'F'],
'col': ['20%', '40%', '80%', '40%', '60%', '20%']})
Also, a dataset similar to this is generated using another dataset which is monthly
d2 = pd.DataFrame({'category': ['A', 'B', 'C', 'D', 'E', 'F'],
'20%': ['2.1', '4.1', '6.8', '5.9', '3.4', '5.4'],
'40%': ['1.9', '3.7', '6.1', '4.8', '4.1', '7.2'],
'60%': ['3.1', '4.9', '6.5', '7.1', '7.9', '5.1'],
'80%': ['2.5', '4.5', '5.6', '6.9', '8.4', '7.4']})
I wish to get an output like this, where it returns the value of element from d2 based on the two columns in d1
op = pd.DataFrame({'category': ['A', 'B', 'C', 'D', 'E', 'F'],
'col': ['20%', '40%', '80%', '40%', '60%', '20%'],
'min_value': ['2.1', '3.7', '5.6', '4.8', '7.9', '5.4']})
I am not sure how to do this look up of values
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 对于undivot,然后将JOIN放在
dataframe.merge
:dataframe.join
和dataframe.stack
:Use
DataFrame.melt
for unpivot and then left join inDataFrame.merge
:Alternative with
DataFrame.join
andDataFrame.stack
: