在 pandas 中将行拆分为多行 [英] Split row into multiple rows in pandas
问题描述
我有一个DataFrame,其格式如下(简化)
I have a DataFrame with a format like this (simplified)
a b 43
a c 22
我希望通过以下方式对此进行分解.
I would like this to be split up in the following way.
a b 20
a b 20
a b 1
a b 1
a b 1
a c 20
a c 1
a c 1
在这里,行数除以20,然后剩下的行数也一样.我有一个解决方案,基本上可以对行进行迭代并填充字典,然后可以将其转换回Dataframe,但我想知道是否有更好的解决方案.
Where I have as many rows as the number divides by 20, and then as many rows as the remainder. I have a solution that basically iterates over the rows and fills up a dictionary which can then be converted back to Dataframe but I was wondering if there is a better solution.
推荐答案
您可以先对模数使用地板分位数,然后通过constructor
与numpy.repeat
.
You can use floor divison with modulo first and then create new DataFrame
by constructor
with numpy.repeat
.
最近需要 numpy.concatenate
,其中list comprehension
表示C
:
a,b = df.C // 20, df.C % 20
#print (a, b)
cols = ['A','B']
df = pd.DataFrame({x: np.repeat(df[x], a + b) for x in cols})
df['C'] = np.concatenate([[20] * x + [1] * y for x,y in zip(a,b)])
print (df)
A B C
0 a b 20
0 a b 20
0 a b 1
0 a b 1
0 a b 1
1 a c 20
1 a c 1
1 a c 1
这篇关于在 pandas 中将行拆分为多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!