【问题标题】:How to read one column values from excel row by row and then write the result in another column in the same excel using Python?如何使用 Python 逐行读取 excel 中的一列值,然后将结果写入同一 excel 中的另一列?
【发布时间】:2023-02-21 23:49:04
【问题描述】:

如何逐行从excel中读取一列值,对这些indivisual值执行一些操作,然后使用Python将结果写入同一个excel中的另一列?

我只能找到如何准备整个 excel / 使用“pandas”阅读整个专栏:)。我是 python 的新手。

我尝试使用 Pandas,它太复杂了,甚至无法获得单个值

【问题讨论】:

  • 请编辑问题以将其限制为具有足够详细信息的特定问题,以确定适当的答案。

标签: python


【解决方案1】:

尝试使用这种方法,首先读取 excel 文件。

    xls = pd.ExcelFile(file_path)
    sheetname='Sheet1'
    df = pd.read_excel(xls, sheetname, header=0)

然后开始收集column A的值,

df['A']

操纵行

df['A'].loc[3]='33.3'

【讨论】:

    【解决方案2】:

    你的问题不是很清楚,但我认为你想一个一个地获取列的行的值并对其应用一些操作,然后将它写入同一 excel 文件中的另一列

    在这里,我使用的数据集有 4 列,第 4 列为空。 现在我将第 3 列的值乘以 2 并将其写入第 4 列。

    # import pandas library
    import pandas as pd
    
    # import excel file 
    df = pd.read_excel('sales.xlsx')
    
    
    for i in range(len(df)):
    df.iloc[i,3]=df.iloc[i,2]*2 # getting value of each row of column 3 multiply it with 2 and store it on same row index in column 3 in datafram
    
    df.to_csv('sales.xlsx') #now save the updated dataframe to same name of excel file
    print(df) #enter code here
    

    运行上述代码后,第 4 列更新,结果为:

    【讨论】:

      猜你喜欢
      • 2017-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多