How to Convert a Python List into an Excel Column: Two Simple Methods
This article shows how to turn a Python list with many elements into the first column of an Excel sheet by presenting two practical solutions—one using openpyxl and another leveraging pandas—complete with code examples and step‑by‑step explanations.
Introduction
Hello, I am a Python enthusiast. Recently a follower asked how to convert a Python list containing many elements into the first column of an Excel spreadsheet. Below is a detailed answer for everyone to learn together.
Solution Process
Two experts provided answers, offering two different methods.
dcpeng's Answer
Method 1 – using openpyxl:
import openpyxl
excel_file = openpyxl.Workbook()
worksheet = excel_file.active
list1 = ['麦当', 'dcpeng', '月神', '王子', '冯诚', '亮哥', '沈复']
for i in range(len(list1)):
worksheet.cell(i+1, 1, list1[i])
excel_file.save('result.xlsx')Method 2 – using pandas for higher efficiency:
import pandas as pd
list1 = ['麦当', 'dcpeng', '月神', '王子', '冯诚', '亮哥']
df = pd.DataFrame(list1)
df.to_excel('666.xlsx')瑜亮's Answer
Additional pandas operations, including printing, transposing, and column manipulation:
import pandas as pd
lst = list(range(10))
print(lst)
df = pd.DataFrame(lst)
print(df)
# df.to_excel('list.xlsx') # save as column
df2 = df.T
print(df2)
# df2.to_excel('list2.xlsx') # save as row
new1 = [9,8,7,6,5,4,3,2,1,0]
new2 = [1,1,1,1,1,2,2,2,2,2]
new3 = [3,3,3,3,3,4,4,4,4,4]
df[0] = new1
df["新"] = new2
col_names = df.columns.tolist()
col_names.insert(0, '新列1')
df3 = df.reindex(columns=col_names, fill_value=0)
print(df3)
df3.insert(0, '新列2', new3)
print(df3)Conclusion
This article, based on a follower's question, demonstrates two concrete ways to convert a Python list into the first column of an Excel file, helping readers solve the problem efficiently. Readers are encouraged to share any additional methods they discover.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
