简体   繁体   English

使用Pandas对现有Excel工作表进行操作

[英]Operations on existing Excel sheet using Pandas

I have two csv files and I have merged the csv files and exported them to an Excel sheet. 我有两个csv文件,我已合并csv文件并将它们导出到Excel工作表。

Now can I add a new column in the existing Excel sheet where the result of the new column will be division operation of two columns existing. 现在我可以在现有的Excel工作表中添加一个新列,其中新列的结果将是现有两列的除法运算。

Example: 例:

col_new=col4/col6

I have tried to implement with csv files where after merging the files the final csv file would be like this: 我试图用csv文件实现,在合并文件之后,最终的csv文件将是这样的:

col1,col2,col3
"1,200",14,2
"1,600",13,4

data=pd.read_csv(filename)

for i,l in zip(data['col1'],data['col3']):

    sah=i.replace(',','')
    sah1=int(float(sah))
    print sah1
    print type(sah1)
    data['res']=sah1/l
    print data

Expected: 预期:

col1    col2   col3   res
---------------------------
1,200    14      2    600
1,600    13      4    400

You can convert the first column to an integer using str.replace(',','') as you have done and then use pd.to_numeric() to recast the entire series at once. 您可以使用str.replace(',','')将第一列转换为整数,然后使用pd.to_numeric()一次重铸整个系列。 Now that you have the two columns you're interested in as integers, just use the ability to divide one series by another and store that in res . 现在你有两个你感兴趣的列作为整数,只需使用将一个系列除以另一个系列的能力并将其存储在res From there, you can export it to a csv or excel file. 从那里,您可以将其导出到csv或excel文件。

# set up the DataFrame to match your input
df = pd.DataFrame([["1,200", 14, 2],["1,600", 13, 4]], columns=['col1','col2','col3'])
print(df)
#     col1  col2  col3
# 0  1,200    14     2
# 1  1,600    13     4

df['res'] = pd.to_numeric(df.col1.str.replace(',','')) / df.col3
print(df)
#     col1  col2  col3    res
# 0  1,200    14     2  600.0
# 1  1,600    13     4  400.0

Part of the issue I see in your code is data['res']=sah1/l is saying data['res'] is a scalar. 我在你的代码中看到的部分问题是data['res']=sah1/l表示data['res']是一个标量。 So it will fill the entire column with that singular value rather than evaluating row wise. 因此,它将使用该奇异值填充整个列,而不是逐行评估。 It will do this each iteration which is why you see the res column the same every time it prints. 它会在每次迭代时执行此操作,这就是为什么每次打印时都会看到res列相同的原因。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 "使用 python pandas 将现有的 excel 表附加到新的数据框" - Append existing excel sheet with new dataframe using python pandas 如何使用 Pandas 在现有 excel 文件中保存新工作表? - How to save a new sheet in an existing excel file, using Pandas? 如何使用熊猫将工作表添加到现有的Excel文件中? - How to add sheet to existing excel file with pandas? 无法将Pandas Dataframe附加到现有的Excel工作表 - unable to append pandas Dataframe to existing excel sheet 将 Pandas 数据帧放入现有的 Excel 表 - Put Pandas Data Frame to Existing Excel sheet 使用 Pandas 合并 2 Excel 电子表格到现有 Excel 电子表格中的新工作表时删除索引列 - Removing the Indexed Column when Merging 2 Excel Spreadsheets into a new Sheet in an existing Excel Spreadsheet using Pandas Pandas 在尝试 append 到现有工作表时创建新的 excel 工作表 - Pandas creates new excel sheet when trying to append to existing sheet 如何使用Pandas将python Web抓取数据导出到现有excel文件中的特定工作表? - How can I export my python web scrape data to a specific sheet in an existing excel file using pandas? 使用python熊猫将现有的Excel工作表与新的数据框追加到新的数据框,而无需加载旧的 - Append existing excel sheet with new dataframe using python pandas without loading the old one 无法使用 pandas 将完整的 pivot 表写入现有 excel 工作簿中的新工作表 - Unable to write full pivot table to a new sheet in an existing excel workbook using pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM