首页 > 解决方案 > ValueError:没有要连接的对象

问题描述

我正在尝试合并不同的txt。我想为每个不同的目标创建不同的数据集。

path1 = r'C:\Users\chiar\Desktop\Dataset\A' # use your path
all_files1 = glob.glob(path1 + "/*.csv")
name= ['quat_1', 'quat_2', 'quat_3', 'quat_4']
a = pd.concat((pd.read_csv(f, names=name) for f in all_files), ignore_index=True)
a['activity'] = 'A'

path2 = r'C:\Users\chiar\Desktop\TESI\Dataset\B' # use your path
all_files2 = glob.glob(path2 + "/*.csv")
name= ['quat_1', 'quat_2', 'quat_3', 'quat_4']
b = pd.concat((pd.read_csv(f, names=name) for f in all_files2), ignore_index=True)
b['activity'] = 'B'

path3 = r'C:\Users\chiar\Desktop\TESI\Dataset\C' # use your path
all_files3 = glob.glob(path3 + "/*.csv")
name= ['quat_1', 'quat_2', 'quat_3', 'quat_4']
c = pd.concat((pd.read_csv(f, names=name) for f in all_files3), ignore_index=True)
c['activity'] = 'C'

它适用于前 5 行,它使用正确的值和标签创建了一个名为 A 的 DataFrame,但是对于第二个和第三个 DataFrame(b 和 c),我得到了这个错误:

ValueError:没有要连接的对象

有人可以在这里帮助我吗?非常感谢!

标签: pythonpandas

解决方案


首先,您正在迭代all_files(unknown) 而不是all_files1. 所以这可以解释为什么前 5 行有效。

然后,你在一个 Windows 文件系统上,所以层次分隔符 is\和 not /,你应该os.path.join改用(所以你不必担心用户的文件系统):

import os
all_files1 = glob.glob(os.path.join(path1, "*.csv"))

推荐阅读