我有以下输入和用例,注意索引是数组,当len大于1时,就意味着广播:
import pandas as pd
df = pd.DataFrame([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]],
index=pd.Index([[1], [2, 3], [4]]),
columns=['a', 'b', 'c'])
print(df)并希望以一种广播以下值的方式使索引变平:
expected = pd.DataFrame([[1, 2, 3],
[4, 5, 6],
[4, 5, 6],
[7, 8, 9]],
index=[1, 2, 3, 4],
columns=['a', 'b', 'c'])
print(expected) 发布于 2022-08-17 12:16:42
您可以暂时将索引设置为列,将其设置为explode,并将其设置为索引:
df.reset_index().explode('index').set_index('index')产出:
a b c
index
1 1 2 3
2 4 5 6
3 4 5 6
4 7 8 9https://stackoverflow.com/questions/73388298
复制相似问题