在python中从excel文件导入数据帧时出现类型错误
我正在尝试将pandas数据框保存为excel文件,然后再次导入并将其转换回字典。数据帧的大小相当大。例如,考虑下面的代码:在python中从excel文件导入数据帧时出现类型错误,python,pandas,Python,Pandas,我正在尝试将pandas数据框保存为excel文件,然后再次导入并将其转换回字典。数据帧的大小相当大。例如,考虑下面的代码: import pandas as pd path = 'file.xlsx' dict1 = {'a' : [3, [1, 2, 3], 'text1'], 'b' : [4, [4, 5, 6, 7], 'text2']} print('\n\nType 1:', type(dict1['a'][1])) df1 = pd.DataFrame(di
import pandas as pd
path = 'file.xlsx'
dict1 = {'a' : [3, [1, 2, 3], 'text1'],
'b' : [4, [4, 5, 6, 7], 'text2']}
print('\n\nType 1:', type(dict1['a'][1]))
df1 = pd.DataFrame(dict1)
df1.to_excel(path, sheet_name='Sheet1')
print("\n\nSaved df:\n", df1 , '\n\n')
df2 = pd.read_excel(path, sheet_name='Sheet1')
print("\n\nLoaded df:\n", df2 , '\n\n')
dict2 = df2.to_dict(orient='list')
print("New dict:", dict2, '\n\n')
print('Type 2:', type(dict2['a'][1]))
输出为:
Type 1: <class 'list'>
Saved df:
a b
0 3 4
1 [1, 2, 3] [4, 5, 6, 7]
2 text1 text2
Loaded df:
a b
0 3 4
1 [1, 2, 3] [4, 5, 6, 7]
2 text1 text2
New dict: {'a': [3, '[1, 2, 3]', 'text1'], 'b': [4, '[4, 5, 6, 7]', 'text2']}
Type 2: <class 'str'>
类型1:
保存的df:
a b
0 3 4
1 [1, 2, 3] [4, 5, 6, 7]
2文本1文本2
加载的df:
a b
0 3 4
1 [1, 2, 3] [4, 5, 6, 7]
2文本1文本2
新的格言:{'a':[3',[1,2,3],'text1'],'b':[4',[4,5,6,7],'text2']}
第2类:
你能帮我找回相同元素类型的原始字典吗?
谢谢大家! 现在,有一个带有
read\u excel
的选项,允许我们在读取列时更改列的dtype
,但是没有这样的选项来更改任何行的dtype
。因此,在读入数据之后,我们必须自己进行类型转换
正如您在问题中所示,df['a'][1]
具有类型str
,但您希望它具有类型列表
因此,假设我们有一些字符串l='[1,2,3]'
我们可以将其转换为一个int(l=[1,2,3]
)列表,作为[int(val)表示l.strip('[]').split(',')]
中的val。现在,我们可以将其与结合使用。应用方法来获得我们想要的:
df.iloc[1]=df.iloc[1]。应用(lambda x:[int(val)表示x.strip(“[]”)中的val。拆分(“,”))
把这个例子放在一起,我们有:
import pandas as pd
# Data as read in by read_excel method
df2 = pd.DataFrame({'a' : [3, '[1, 2, 3]', 'text1'],
'b' : [4, '[4, 5, 6, 7]', 'text2']})
print('Type: ', type(df2['a'][1]))
#Type: <class 'str'>
# Convert strings in row 1 to lists
df2.iloc[1] = df2.iloc[1].apply(lambda x : [int(val) for val in x.strip('[]').split(',')])
print('Type: ', type(df2['a'][1]))
#Type: <class 'list'>
dict2 = df2.to_dict(orient='list')
将熊猫作为pd导入
#通过读取excel方法读取的数据
df2=pd.DataFrame({'a':[3',[1,2,3]','text1'],
‘b’:[4,[4,5,6,7],‘text2']})
打印('类型:',类型(df2['a'][1]))
#类型:
#将第1行中的字符串转换为列表
df2.iloc[1]=df2.iloc[1]。应用(λx:[int(val)表示x.strip(“[]”)中的val。拆分(“,”))
打印('类型:',类型(df2['a'][1]))
#类型:
dict2=df2.to_dict(orient='list')