[英]Adding a new column to a dataframe based on the values of another dataframe
I do have two csv
files, I am using pandas to read the data.我确实有两个
csv
文件,我正在使用 pandas 读取数据。
The train.csv
contains values, with headers id, sentiment
train.csv
包含值,标头id, sentiment
87,Positive
10,Positive
7,Neutral
The text.csv
contains values, with headers id, text
text.csv
包含值,标题id, text
7,hello, I think the price if high...
87, you can call me tomorow...
....
I would like to insert the text from text.csv
into train.csv
so the result would be:我想将
train.csv
text.csv
结果是:
87,Positive, you can call me tomorow...
Can any one help with pandas?任何人都可以帮助 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
Some Ids may not be in the files, so I need to set null if the id does not exists Note
有些id可能不在文件中,所以如果id不存在我需要设置null
set the indices on the two dataframes, then add the columns:在两个数据帧上设置索引,然后添加列:
train.set_index('id').sentiment + text.set_index('id').text
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.