Python拆分给定的列表并插入EXCEL文件中
给出一个连续包含姓名和地址的列表,任务是一次分割这两个元素并将其插入excel中。
我们可以使用一个非常流行的数据分析库,Pandas。使用Pandas,我们可以很容易地处理这些列,并使用df.to_excel()函数将过滤后的元素简单地插入到excel文件。
以下是实现情况。
# Python code to split the list two element
# at a time and insert it into excel.
# Importing pandas as pd
import pandas as pd
# List initialization
list1 = ['Assam', 'India',
'Lahore', 'Pakistan',
'New York', 'USA',
'Bejing', 'China']
df = pd.DataFrame()
# Creating two columns
df['State'] = list1[0::2]
df['Country'] = list1[1::2]
# Converting to excel
df.to_excel('result.xlsx', index = False)
输出 :

极客教程