首页 > 解决方案 > 循环遍历excel列并在python中写入新值

问题描述

我在 python 中使用 pandas,并且有一个包含一个名为 test_name 的标题的列的 excel 文件。我想遍历该列的每一行并更新值。我已经编写了从列中提取每个值的代码,但我不确定如何将新值写回单元格。

import pandas as pd

orders = pd.read_excel('test.xlsx')

if orders.columns[0] == 'test_name':
    cols = [0]
    test_names = pd.read_excel('test.xlsx', usecols=cols)

    for name in test_names.values:
        print(name[0])
        
        new_value = 3333
        #write new_value back to the current cell.

else:
    print('Column test_name not found ')

标签: pythonpandas

解决方案


推荐阅读