Home > Software engineering >  Im trying to write the data in the list into an excel sheet,and the list is displayed as shown below
Im trying to write the data in the list into an excel sheet,and the list is displayed as shown below

Time:10-16

so the when I just display list out its showed as:

A     22
WO     8
Name: 7, dtype: int64

and when I try list[0] it only prints the value 22.

how do I print the whole row like:

A   22

Also I'm trying to write this into an excel sheet. And im thinking to make a separate columns for A and WO with their corresponding values in the rows. Please let me know how to achieve that

CodePudding user response:

Here is a step-by-step guide to writing lists to excel using pandas and printing the first row in the data frame (or all rows).

import pandas as pd

list_1 = [0, 1, 2]
list_2 = ['A', 'B', 'C']
data_as_dictionary = {'Name':list_2, 'Value':list_1}
df = pd.DataFrame(data_as_dictionary)
# write df into excell  
df.to_excel('df_as_excel.xlsx', index=None)   
# print first row from the df 
print(df.loc[[0]])

# if you already have an excel and want to print the rows:
# 1. load in excel 
df2 = pd.read_excel('df_as_excel.xlsx', index_col=None)
# 2. print first row
print(df2.loc[[0]])
# 3. or if you want to print all rows in the df2
for row_number in range(len(df2)):
    print(df2.loc[[row_number]])

  • Related