我正在尝试将列表中的数据写入excel sheet,列表显示如下 below.If 我尝试列表[0] 我只得到22,如何得到A ?
Im trying to write the data in the list into an excel sheet,and the list is displayed as shown below.If i try list[0] i only get 22, how to get A too?
所以当我刚刚显示列表时,它显示为:
A 22
WO 8
Name: 7, dtype: int64
当我尝试 list[0] 时,它只打印值 22。
如何像这样打印整行:
A 22
我也在尝试将其写入 excel sheet。我正在考虑为 A 和 WO 创建一个单独的列,并在行中显示相应的值。请让我知道如何做到这一点
这是使用 pandas 将列表写入 excel 并打印数据框中的第一行(或所有行)的分步指南。
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]])
所以当我刚刚显示列表时,它显示为:
A 22
WO 8
Name: 7, dtype: int64
当我尝试 list[0] 时,它只打印值 22。
如何像这样打印整行:
A 22
我也在尝试将其写入 excel sheet。我正在考虑为 A 和 WO 创建一个单独的列,并在行中显示相应的值。请让我知道如何做到这一点
这是使用 pandas 将列表写入 excel 并打印数据框中的第一行(或所有行)的分步指南。
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]])