【问题标题】:Python Excel: how to add a number to each cell in an existing excel file and then send the new value back to excel documentPython Excel:如何向现有excel文件中的每个单元格添加一个数字,然后将新值发送回excel文档
【发布时间】:2021-02-02 01:15:49
【问题描述】:

如何在现有 Excel 文件的每个单元格中添加一个数字,然后将新值发送回 Python 中的 Excel 文档?

这是我目前拥有的:

for infile in glob2.glob("*.xlsx"):
    file, ext = os.path.splitext(infile)
    print(file)

Filename=str(file)+'.xlsx'
df=pd.read_excel(Filename, index_col=0,parse_dates=True)

#Tell it what columns to read from each file
vals41 = (df['IN/OUT4_1'])
vals41 =(df['IN/OUT4_2'])
vals43=(df['IN/OUT4_3']) 

我想将 14.7 添加到这 3 列中的每个单元格,然后将新值发送回文件。

【问题讨论】:

  • 你试过什么?为什么它不起作用?我们希望您在 Stack Overflow 上发帖之前做出诚实的尝试。

标签: python excel pandas filenames


【解决方案1】:

如果我正确理解了您的要求,您正在尝试更新 excel 中 3 个特定列的值。你可以试试下面的代码:

for infile in glob2.glob("*.xlsx"):
    file, ext = os.path.splitext(infile)
    print(file)

Filename=str(file)+'.xlsx'
df=pd.read_excel(Filename, index_col=0,parse_dates=True)

#Tell it what columns to read from each file
cols=['IN/OUT4_1','IN/OUT4_2','IN/OUT4_3'] 
df[cols] = df[cols]+14.7

df.to_excel(r'Path to store the exported excel file\File Name.xlsx', index = False)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多