【问题标题】:How to concatenate multiple data frames with long index without error?如何连接多个具有长索引的数据帧而不会出错?
【发布时间】:2019-10-12 14:03:07
【问题描述】:

我有一个目录“.../dados”,其中有多个子目录,其名称是序列号加上一些无用的信息 - 例如“17448_2017_Jul_2017_Oct”,其中第一个数字是序列号。在每个子目录中,我有四个“.txt”文件,它们的行/行包含日期和时间信息,以及某种类型的属性,比如湿度,在每个子目录中都以相同的方式命名 - 例如“2019-01-29 03:11:26 54.7”。

我想连接所有这些以生成具有日期索引的数据集。

path = "/.../dados/"

df = pd.DataFrame()

for fld in os.listdir(path):
    subfld = path + fld
    if os.path.isdir(subfld):
        aux = pd.DataFrame()
        sn = fld.split('_')[0]
        for file in os.listdir(subfld):
            filepath = os.path.join(subfld, file)
            if os.path.isfile(filepath):
                new_col = pd.read_fwf(filepath, colspecs=[(0, 19), (20, -1)], skiprows=8, names=[file.split('_')[2][:-4]], parse_dates=[0], nrows=9999999)
                aux = pd.concat([aux, new_col], axis=1,  sort=False)
        aux['Machine'] = sn
        df = df.append(aux)

这是 df.head(10) 的打印:

HumTechRoom  TempTechRoom  TempExamRoom  HumExamRoom Machine
2018-03-04 00:45:11         82.6           NaN           NaN          NaN   22162
2018-03-04 00:45:47         80.0           NaN           NaN          NaN   22162
2018-03-04 00:45:53         78.0           NaN           NaN          NaN   22162
2018-03-04 00:46:04         75.9           NaN           NaN          NaN   22162
2018-03-04 00:46:20         73.7           NaN           NaN         51.3   22162
2018-03-04 00:46:58         71.7           NaN           NaN          NaN   22162
2018-03-04 00:47:40          NaN           NaN           NaN         53.4   22162
2018-03-04 00:47:41          NaN          14.5           NaN          NaN   22162
2018-03-04 00:47:54         74.3           NaN           NaN          NaN   22162
2018-03-04 00:47:59         76.6           NaN           NaN          NaN   22162

这是我收到的错误消息:

...
line 31, in <module>
    aux = pd.concat([aux, new_col], axis=1,  sort=False)

  File ".../concat.py", line 226, in concat
    return op.get_result()

  File ".../concat.py", line 423, in get_result
    copy=self.copy)

  File ".../internals.py", line 5425, in concatenate_block_managers
    return BlockManager(blocks, axes)

  File ".../internals.py", line 3282, in __init__
    self._verify_integrity()

  File ".../internals.py", line 3493, in _verify_integrity
    construction_error(tot_items, block.shape[1:], self.axes)

  File ".../internals.py", line 4843, in construction_error
    passed, implied))

ValueError: Shape of passed values is (2, 19687), indices imply (2, 19685)

【问题讨论】:

  • 你的问题是什么?
  • @Erfan 我该如何解决这个错误...
  • 你试过用谷歌搜索错误信息吗?我刚刚做到了,我得到的点击数比我想象的要多。概括地说,一个 DataFrame 中有 19687 列,另一个 DataFrame 中有 19685 列,相差两列。因此,DataFrames 不能被连接。
  • 您可能有重复的索引值。试试:df.drop_duplicates(inplace=True)
  • 然后删除axis=1 中的pd.concat

标签: python pandas dataframe concat valueerror


【解决方案1】:

您似乎在错误的轴上使用了pd.concat。从pd.concat.. 行中删除axis=1,因为axis=0 是默认值,可以在docs 中找到

只是为了您的方便。要获得更清晰的数据框,请同时使用ignore_index=True

aux = pd.concat([aux, new_col], ignore_index=True,  sort=False)

这会返回一个重置索引。

【讨论】:

  • 很高兴我能帮上忙,又添加了一个提示,您可以测试一下是否喜欢@MarlonHenriqueTeixeira
【解决方案2】:

您的 DataFrame 的形状不兼容:

ValueError: Shape of passed values is (2, 19687), indices imply (2, 19685)

换句话说,问题在于 19687 != 19685。无论您遇到什么答案都将来自您的数据的具体情况,考虑到数据的大小,这可能不适合分享。您至少需要在某处添加或删除 2 行。您需要进行调查以确定什么和在哪里。

【讨论】:

  • 考虑到差异是 2,可能性是你的头部和尾部。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-27
  • 2019-11-02
  • 2017-09-04
  • 2021-01-12
  • 2017-03-05
  • 2020-01-06
  • 1970-01-01
相关资源
最近更新 更多