假设一个名为fruits
的sql表包含以下数据
id name color
1 apple red
2 apple green
3 orange orange
4 banana yellow
在python中,可以通过
^{pr2}$现在我需要一个方法get_fruit_id(df)
,它根据df
输入从表中返回id。因此,df
由
color name
red apple
green apple
yellow melon
我希望get_fruit_id(df)
返回类似(1, 2, NaN)
或
color name id
red apple 1
green apple 2
一个可能的解决方案是从水果表中提取所有数据并使用熊猫。合并,但我要处理大表,所以我会优先考虑性能。提取所有数据对我来说似乎是不必要的。在
使用df的示例可以通过
df = pd.DataFrame({
'name': ['apple']*2 + ['melon'],
'color': ['red', 'green', 'yellow']
})
目前没有回答
相关问题 更多 >
编程相关推荐