我想同时将列和默认短语中的数据附加到Pandas DataFrame中,db
有很多列,不仅是id_sin&extra ...
我尝试过:
import pandas as pd
db = pd.DataFrame({'id_sin':['s123','s124','s125','s126'],
'extra':['abc','def','ghi','jkl'],
...
})
df = pd.DataFrame(columns=['id_sin','phrase'])
df = df.append([db['id_sin'],['default phrase']*len(db)])
这是预期的结果:
>>> df
id_sin phrase
0 s123 default phrase
1 s124 default phrase
2 s125 default phrase
3 s126 default phrase
但是我遇到了错误,请帮忙...谢谢!
答案 0 :(得分:1)
您可以创建现有DataFrame的副本,然后向其添加新列。
import pandas as pd
db = pd.DataFrame({'id_sin':['s123','s124','s125','s126'],
'extra':['abc','def','ghi','jkl'],
...
})
df = pd.DataFrame()
df['id_sin'] = db[['id_sin']]
df['Phrase'] = 'Default Phrase'