使用Pandas和DataFrame在CSV文件中添加新列

时间:2019-05-02 08:51:32

标签: pandas csv dataframe concat df

我想通过从两个不同的字典中添加新的标题和内容来重写csv文件,但这似乎不起作用:

df = pd.read_csv(sys.argv[1], na_values = ['no info', '.'], encoding='Cp1252', delimiter=';')
f=pd.DataFrame(d_filtered_words).T.reset_index()
f.columns=['id','filtered_words']
df=pd.concat([df,f],axis=1)
df.to_csv('result.csv', sep=';', encoding='Cp1252')

df = pd.read_csv('result.csv', na_values = ['no info', '.'], encoding='Cp1252', delimiter=';')
s=pd.DataFrame(d_score).T.reset_index()
s.columns=['id','ScorePos','Scoreneg','Scoreneu']
df=pd.concat([df,s],axis=1)
df.to_csv('result2.csv', sep=';', encoding='Cp1252')

输入文件如下所示(太长了,我只写一个例子:

numero;nom
1;marie
2;noemie

预期结果

numero;nom;id;filtered_words;id;ScorePos;Scoreneg;Scoreneu
1;marie;1;marie;1;25;58;569
2;noemie;2;noemie;56;789;25

代码错误:

dico中的值不相同,但是正常情况下它们包含不同的单词数,我无法更改它们,数据就是这样

    s=pd.DataFrame(d_filtered_words).T.reset_index()
  File "C:\Users\Em\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\frame.py", line 348, in __init__
    mgr = self._init_dict(data, index, columns, dtype=dtype)
  File "C:\Users\Em\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\frame.py", line 459, in _init_dict
    return _arrays_to_mgr(arrays, data_names, index, columns, dtype=dtype)
  File "C:\Users\Em\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\frame.py", line 7356, in _arrays_to_mgr
    index = extract_index(arrays)
  File "C:\Users\Em\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\frame.py", line 7402, in extract_index
    raise ValueError('arrays must all be same length')
ValueError: arrays must all be same length

0 个答案:

没有答案