无法合并对象列类型上的两个数据帧集

2024-09-28 17:29:35 发布

您现在位置:Python中文网/ 问答频道 /正文

merged_df = file1.merge(file1, file2, on="WineType")

ValueError: unknown type str160

file2.info()

<class ‘pandas.core.frame.DataFrame’>
RangeIndex: 4898 entries, 0 to 4897
Data columns (total 13 columns):

Column Non-Null Count Dtype
0 fixed acidity 4898 non-null float64
1 volatile acidity 4898 non-null float64
2 citric acid 4898 non-null float64
3 residual sugar 4898 non-null float64
4 chlorides 4898 non-null float64
5 free sulfur dioxide 4898 non-null float64
6 total sulfur dioxide 4898 non-null float64
7 density 4898 non-null float64
8 pH 4898 non-null float64
9 sulphates 4898 non-null float64
10 alcohol 4898 non-null float64
11 quality 4898 non-null int64
12 WineType 4898 non-null object
dtypes: float64(11), int64(1), object(1)
memory usage: 497.6+ KB

file1.info()

<class ‘pandas.core.frame.DataFrame’>
RangeIndex: 1599 entries, 0 to 1598
Data columns (total 13 columns):

Column Non-Null Count Dtype
0 fixed acidity 1599 non-null float64
1 volatile acidity 1599 non-null float64
2 citric acid 1599 non-null float64
3 residual sugar 1599 non-null float64
4 chlorides 1599 non-null float64
5 free sulfur dioxide 1599 non-null float64
6 total sulfur dioxide 1599 non-null float64
7 density 1599 non-null float64
8 pH 1599 non-null float64
9 sulphates 1599 non-null float64
10 alcohol 1599 non-null float64
11 quality 1599 non-null int64
12 WineType 1599 non-null object
dtypes: float64(11), int64(1), object(1)
memory usage: 162.5+ KB

您能告诉我,如何将两个数据帧合并到一个带有公共列的文件中吗。 提前谢谢


Tags: columnsinfoobjectnullfile1classfile2total
2条回答

您正试图在object数据类型上进行合并pandas不知道如何正确匹配这些字段中的值。在这两种情况下,您都可以尝试转换为字符串,例如

file1['WineType'] = file1['WineType'].astype(str) 
file2['WineType'] = file2['WineType'].astype(str)
merged_df = file1.merge(file1, file2, on="WineType")

然后合并应该会成功

你也可以试试.astype('category')。有关列类型转换的详细信息: Change column type from string to float in Pandas

我也尝试过这种方法。 concat_df = pd.concat([file1, file2]).reset_index(level=0, drop=True)而且效果很好。 我也尝试了下面的一个,它的工作很好

file1['WineType'] = file1['WineType'].astype(str) 
file2['WineType'] = file2['WineType'].astype(str)
merged_df = file1.merge(file1, file2, on="WineType")

相关问题 更多 >