我有一个Excel文件,该文件的列包含列表(请参见图片)。使用pandas.read_excel()导入列的正确方法是什么?
我最终需要能够为列表的每个值创建一个带有一列的数据框。
这是我认为我需要做的,但这是不正确的。
# read in the file
df = pd.read_excel('fruit.xlsx')
df
# this isn't working...
# create new dataframe with column for each value of the "Fruit" column in df
fruits = df['Fruit'].apply(pd.Series)
fruits
但是,当我从字典创建初始数据帧而不是读取Excel文件时,以下内容确实起作用。
read_excel()在做什么? 如何指定excel列为列表?
# dictionary with the same data as the excel file
raw_data = {'ID': [1, 2, 3, 4],
'Fruit': [['Apple', 'Banana', 'Pear'],
['Pineapple'],
'',
['Apple', 'Orange']]}
# dataframe from the dictionary
df = pd.DataFrame(raw_data, columns=['ID', 'Fruit'])
df
# new dataframe with a column for each value of the lists
fruits = df['Fruit'].apply(pd.Series)
fruits
谢谢。