Python | Split given list and paste into Excel file



We can use a very popular library for data analysis, Pandas. Using pandas, we can easily manipulate the columns and just insert the filtered items into the Excel file using the df.to_excel () function.

Below is the implementation:

Output:


# Python code to split the list into two elements
# and paste it into Excel.

 
# Import pandas as pd

import pandas as pd

  
# Initialization list

list1 = [ ` Assam` , `India` ,

  ` Lahore` , `Pakistan`

`New York` , `USA` ,

`Bejing` , ` China` ]

 

df = pd.DataFrame ()

 
# Create two columns

df [ `State` ] = list1 [ 0 :: 2 ]

df [ `Country` ] = list1 [ 1 :: 2 ]

 
# Excel conversion

df.to_excel ( `result.xlsx` , index = False )