如果数据框具有相同的索引,如何重新塑造数据框?

3

如果我有一个像这样的数据框:

df= pd.DataFrame(['a','b','c','d'],index=[0,0,1,1])
   a  b
0  0  0
1  c  d

我该如何根据索引重塑数据帧,使其像上面那样呈现,即:

df= pd.DataFrame([['a','b'],['c','d']],index=[0,1])
  0  1
0  a  b
1  c  d

你需要我提供什么? - cs95
如果我有多列,这个方法就不起作用了。这与另一个问题非常相似。 - Bharath M Shetty
我会尝试提出新的问题。 - Bharath M Shetty
3个回答

6

让我们使用set_indexgroupbycumcountunstack

df.set_index(df.groupby(level=0).cumcount(), append=True)[0].unstack()

输出:

   0  1
0  a  b
1  c  d

3
跟我一模一样的字符......除了我在逗号后面加了一个空格,符合PEP标准等(-:)。 - piRSquared
1
你不会相信,我也有同样的解决方案,但是你比我快... ;) - MaxU - stand with Ukraine
1
感谢您的回答,先生。 - Bharath M Shetty
@Scott Boston,您能尝试一下多列的解决方案吗?我现在卡住了。 - Bharath M Shetty
@ScottBoston 我将它作为一个单独的问题发布了。https://dev59.com/BKXja4cB1Zd3GeqPSouk - Bharath M Shetty

3
你可以使用cumcount来配合pivot:
a = df.groupby(level=0).cumcount()
df = pd.pivot(index=df.index, columns=a, values=df[0])

如果我有多个列,这种方法就行不通了。有没有改进的方法? - Bharath M Shetty

2
有几种方法。 1.
In [490]: df.groupby(df.index)[0].agg(lambda x: list(x)).apply(pd.Series)
Out[490]:
   0  1
0  a  b
1  c  d

2.

In [447]: df.groupby(df.index).apply(lambda x: pd.Series(x.values.tolist()).str[0])
Out[447]:
   0  1
0  a  b
1  c  d

3.

In [455]: df.assign(i=df.index, c=df.groupby(level=0).cumcount()).pivot('i', 'c', 0)
Out[455]:
c  0  1
i
0  a  b
1  c  d

移除名称

In [457]: (df.assign(i=df.index, c=df.groupby(level=0).cumcount()).pivot('i', 'c', 0)
           .rename_axis(None).rename_axis(None, 1))
Out[457]:
   0  1
0  a  b
1  c  d

谢谢John让我知道这些选项。 - Bharath M Shetty
当涉及到多列时,我卡住了。 - Bharath M Shetty

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接