如何将多个字典添加到pandas中的现有数据帧

2024-09-28 17:15:17 发布

您现在位置:Python中文网/ 问答频道 /正文

你好,我有一个数据框,我想添加两个新的字典了。我可以添加一个,但它会覆盖现有数据,不会添加另一个。我怎样才能解决这个问题

这就是我目前正在尝试的,它执行计算并将创建新文件,但它只创建一列,不包含任何现有数据。我怎样才能解决这个问题,这可能吗

谢谢

#Fill the data from excel
data = pd.read_excel('file.xlsx')
df = pd.DataFrame(data)
#Perform calculations
new_col_a = np.multiply(df['somecol'], x_rate) 
new_col_b = np.subtract(new_col_a, df['somecol'])
#create the new columns
df['col_a'] = new_col_a
df['col_b] = new_col_b
#now create new excel file
df.to_excel('new.xlsx')

Tags: 文件the数据dfnewdata字典create