【问题标题】:Python & Excel: Append a list as column (specific column and row) into existing excelPython和Excel:将列表作为列(特定列和行)附加到现有的excel中
【发布时间】:2021-04-24 13:50:13
【问题描述】:

我在python中遇到了一个问题。

我想将一列(列表)附加到现有的 Excel 中。

我将我的 excel 更改为数据框,但我不知道如何将列表附加到其中。

原文:

我想追加列表。但是,我的行从 8 而不是 1 开始。

我尝试了下面的代码,但遇到了错误。

 df1 = read_excel(file_name, sheet_name=my_sheet, engine='openpyxl')
 h = [2,3,4,5]
 df1['List'] = h
ValueError: Length of values (4) does not match length of index (14)

预期结果:

感谢任何帮助。谢谢

【问题讨论】:

    标签: python excel pandas dataframe


    【解决方案1】:

    您的 excel 包含空行,而您的 python 正在读取它们。 为了确保 - 打印出你的 df:

    display(df1)
    

    如果是这种情况,请尝试过滤掉它们:

    df1 = read_excel(file_name, sheet_name=my_sheet, engine='openpyxl', na_filter=True)
    

    或者,您可以在阅读 excel 后删除空行:

    df1 = read_excel(file_name, sheet_name=my_sheet, engine='openpyxl')
    df1 = df1.dropna()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-25
      • 2020-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多