0
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

Can you please guide me, how to merge both dataframe in a single file with the common column. thanks in advance.

sophros
  • 14,672
  • 11
  • 46
  • 75
vivek rajagopalan
  • 843
  • 3
  • 13
  • 22

2 Answers2

1

You are trying to merge on an object data type. pandas has no clue how to properly match values from these field. You can try converting to string in both cases, e.g.

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

And then the merge should be successful.

You can also try .astype('category'). More on the column type conversions: Change column type from string to float in Pandas

sophros
  • 14,672
  • 11
  • 46
  • 75
0

i have tried this method also. concat_df = pd.concat([file1, file2]).reset_index(level=0, drop=True) and worked fine. I have tried the below one also and its working fine.

file1['WineType'] = file1['WineType'].astype(str) 
file2['WineType'] = file2['WineType'].astype(str)
merged_df = file1.merge(file1, file2, on="WineType")
vivek rajagopalan
  • 843
  • 3
  • 13
  • 22