DarknessPlusPlus
DarknessPlusPlus

Reputation: 563

ValueError: can not merge DataFrame with instance of type <class 'str'>

I am trying to merge two datasets that have the same column 'Value' but it gives me this error : can not merge DataFrame with instance of type

import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.pyplot import pie, axis, show
from pandas import Series, DataFrame

class Dataset():
    def __init__(self, input):
        self.choice = input
        self.file = 0

def read(self):
    if self.choice == ("merge"):
        self.file = pd.read_csv('bbc.csv')
        self.file = pd.read_csv('cnn.csv')
        print(pd.merge('bbc.csv', 'cnn.csv', on="Value"))

Upvotes: 0

Views: 4356

Answers (1)

trouselife
trouselife

Reputation: 969

You have defined the two opened .csv as self.file... and then you're trying to merge two strings. Instead, define the dataframes as variables and then merge:

if self.choice == ("merge"):
    self.file1 = pd.read_csv('bbc.csv')
    self.file2 = pd.read_csv('cnn.csv')
    # print(pd.merge(self.file1, self.file2, on="Value"))
    print(self.file1.merge(self.file2, how='inner', on="Value"))

Upvotes: 1

Related Questions