ValueError:无法将DataFrame与类型为<class'str'=“”>的实例合并

时间:2018-03-10 20:47:11

标签: python python-3.x pandas

我正在尝试合并两个具有相同列'Value'的数据集,但它给了我这个错误:无法将DataFrame与类型

的实例合并
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"))

1 个答案:

答案 0 :(得分:1)

您已将两个打开的.csv定义为self.file ...然后您尝试合并两个字符串。相反,将数据帧定义为变量然后合并:

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"))