![](/img/trans.png)
[英]Adding a new column to a dataframe from the values of another dataframe based on a condition
[英]Adding a new column to a dataframe based on the values of another dataframe
我確實有兩個csv
文件,我正在使用 pandas 讀取數據。
train.csv
包含值,標頭id, sentiment
87,Positive
10,Positive
7,Neutral
text.csv
包含值,標題id, text
7,hello, I think the price if high...
87, you can call me tomorow...
....
我想將train.csv
text.csv
結果是:
87,Positive, you can call me tomorow...
任何人都可以幫助 pandas 嗎?
import pandas as pd
train= pd.read_csv("train.csv")
text= pd.read_csv("text.csv")
# this does not work
combined= pd.merge(train, text, on=['id'])
Note
有些id可能不在文件中,所以如果id不存在我需要設置null
在兩個數據幀上設置索引,然后添加列:
train.set_index('id').sentiment + text.set_index('id').text
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.