![](/img/trans.png)
[英]Pandas Dataframes: is the value of a column in a list nested in another column, same row?
[英]Joining Pandas Dataframes on Column Name Matches Row Value (with same index)
我有看起來像這樣的數據:
ID Col1 Col2
2018-06-01 'A' 10 100
2018-06-02 'B' 5 25
2018-06-03 'A' 25 25
另一個數據框如下所示:
A B C
2018-06-01 0.5 1.1 1
2018-06-02 0.75 2.1 1
2018-06-03 0.6 0.3 0
我想將它們合並到索引(在這種情況下為日期)匹配且ID列下的行中的值與第二個數據框中的列名匹配的熊貓中。 看起來更簡單,它看起來像這樣:
ID Col1 Col2 NEW
2018-06-01 'A' 10 100 0.5
2018-06-02 'B' 5 25 2.1
2018-06-03 'A' 25 25 0.6
我敢肯定,在熊貓中有一種簡單的方法可以做到這一點,但我想不出會是什么。
使用lookup
df1['New']=df2.lookup(df1.index,df1.ID)
df1
Out[14]:
ID Col1 Col2 New
2018-06-01 A 10 100 0.5
2018-06-02 B 5 25 2.1
2018-06-03 A 25 25 0.6
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.