【问题标题】:ValueError: Shape of passed values is (6, 6), indices imply (4, 6) error when merging two df in pandasValueError:传递值的形状为 (6, 6),在 pandas 中合并两个 df 时,索引暗示 (4, 6) 错误
【发布时间】:2021-11-14 19:45:26
【问题描述】:

我有两个dataframe,如下:

      Background    Skin            Body               Face          Head
value   Beige     Light Gray   TribalNecklace        Beard          Bowl Cut
value   Blue      Normal        BowTie Pink         Blushing        Durag Red. 

          Link
0 https://example.com
1 httpsl//example2.com

当使用以下代码连接两个时:

df5 = pd.concat([df1, linkdf], axis=1, ignore_index=True)
df5

给出一个错误:

ValueError: 传递值的形状为 (6, 6),索引表示 (4, 6)

如何加入两个dataframe 而不会出现该错误?感谢您提前提供帮助。

【问题讨论】:

  • value 的第一列是什么?索引?
  • 它的索引导致我从列更改为行

标签: python pandas


【解决方案1】:

尝试更改您的代码:

df5 = pd.concat([df1, linkdf], axis=1, ignore_index=True)

df5 = pd.concat([df1.reset_index(), linkdf], axis=1)

给你:

   index Background        Skin  ...      Face        Head                  Link
0  value      Beige  Light Gray  ...     Beard    Bowl Cut   https://example.com
1  value       Blue      Normal  ...  Blushing  Durag Red.  httpsl//example2.com

【讨论】:

  • 还有一个问题是有办法让链接可点击
  • 也许this会帮助你
【解决方案2】:

试试assign:

df5 = df1.assign(links=linkdf.values)
>>> df5
      Background        Skin            Body      Face        Head                 links
value      Beige  Light Gray  TribalNecklace     Beard    Bowl Cut   https://example.com
value       Blue      Normal     BowTie Pink  Blushing  Durag Red.  httpsl//example2.com

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-15
    • 2021-12-05
    • 1970-01-01
    • 2019-01-27
    • 2019-02-01
    • 2013-11-09
    • 2020-01-12
    • 2020-05-21
    相关资源
    最近更新 更多