简体   繁体   中英

Python & Excel: Append a list as column (specific column and row) into existing excel

I have encountered a problem in python.

I would want to append a column(list) into an existing excel.

I changed my excel into a dataframe but i do not know how to append the list into it.

Original:

在此处输入图像描述

I want to append the list. However, my rows start from 8 instead of 1.

I have tried the code below but encountered error.

 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)

Expected result:

在此处输入图像描述

Appreciate any help. Thank you

Your excel includes empty rows and your python is reading them. To make sure - print out your df:

display(df1)

If this is the case try filtering them out:

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

Alternatively, you can drop the empty rows after you read the excel:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM