Given a list of Currency and Country, the job is to separate these two parts and enter them into Excel one at a time.

Pandas, a well-known data analysis library, can be used. We can easily alter the columns in pandas and simply put the filtered elements into an excel file using the df.to_excel() function.

Program to Split Given List and Insert in Excel File in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import pandas module as pd using the import keyword.
  • Give the list as static input and store it in a variable.
  • Call the DataFrame() object to create the dataframe and store it in another variable.
  • Create two columns using the above list slicing.
  • Create two columns using the above list slicing.
  • The first column contains the even index values(Currency).
  • The second column contains the odd index values(Country).
  • Convert the above result dataframe columns into an excel file using the to_excel() method by passing the filename and index=False as arguments to it.
  • The Exit of the Program.

Below is the implementation:

# Import pandas module as pd using the import keyword
import pandas as pd
  
# Give the list as static input and store it in a variable.
gvn_list = ['Rupee', 'India',
         'dollar', 'USA', 
         'Yen', 'Japan',
         'Euro', 'France']
  
# Call the DataFrame() object to create the dataframe and store it in another variable.
data_frame = pd.DataFrame()
  
# Create two columns using the above list slicing.
# The first column contains the even index values(Currency)
# The second column contains the odd index values(Country)
data_frame['Currency'] = gvn_list[0::2]
data_frame['Country'] = gvn_list[1::2]
  
# Convert the above result dataframe columns into an excel file using the to_excel() method 
# by passing the filename and index=False as arguments to it.
data_frame.to_excel('demo.xlsx', index = False)

Output:

Python Split given list and insert in excel file

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import pandas module as pd using the import keyword.
  • Give the list as user input using the list(), input() functions and store it in a variable.
  • Call the DataFrame() object to create the dataframe and store it in another variable.
  • Create two columns using the above list slicing.
  • Create two columns using the above list slicing.
  • The first column contains the even index values(Fruits).
  • The second column contains the odd index values(Vegetables).
  • Convert the above result dataframe columns into an excel file using the to_excel() method by passing the filename and index=False as arguments to it.
  • The Exit of the Program.

Below is the implementation:

# Import pandas module as pd using the import keyword
import pandas as pd
  
# Give the list as user input using the list(), input() functions,
# and store the list in a variable.
gvn_list = list(
   input('Enter some random string elements for the list = ').split())

  
# Call the DataFrame() object to create the dataframe and store it in another variable.
data_frame = pd.DataFrame()
  
# Create two columns using the above list slicing.
# The first column contains the even index values(Fruits)
# The second column contains the odd index values(Vegetables)
data_frame['Fruits'] = gvn_list[0::2]
data_frame['Vegetables'] = gvn_list[1::2]
  
# Convert the above result dataframe columns into excel file using the to_excel() method 
# by passing the filename and index=False as arguments to it.
data_frame.to_excel('demo1.xlsx', index = False)

Output:

Enter some random string elements for the list = apple brinjal mango tomato banana beetroot

Python Split given list and insert in excel file user input

Leave a Reply

Your email address will not be published.