本文介绍了按列扩展行,同时根据值处理其他行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似的DataFrame:

I have a DataFrame like so:

  column1  column2  column3
0       a        2        2
1       b        1        0
2       c        3        2

第3列中的值必须< =第2列中的值

Where the value in column3 is necessarily <= the value in column2

我想根据以下规则将df扩展到下面:

I want to expand df to below based on the following rules:

  column1  column2  column3
0       a        1        1
1       a        1        1
2       b        1        0
3       c        1        1
4       c        1        1
5       c        1        0




  1. 每行都扩展为一个数字行等于其在column2中的值

  2. 如果其索引(相对于column1分组)的索引小于column3中原始未展开的行的值,则展开的行的column3的值等于1 。

例如:
看到column1 = a的行是e因为其column2的值等于2,所以将其x分成2行,并且由于0 <0导致两个扩展行在column3中都具有值1。 2和1 < 2。

For example:See that the row with column1=a was expanded into 2 rows because its column2 value was equal to 2, and both the resulting expansion rows have a value of 1 in column3 because 0 < 2 and 1 < 2.

请注意,由于column2的值等于3,所以column1 = c的行被扩展为3行,但是,只有前2个扩展行具有column3中的值1(再次:0

See that the row with column1=c was expanded into 3 rows because its column2 value was equal to 3, however, only the first 2 resulting expansion rows have a value of 1 in column3 (again: 0 < 2 and 1 < 2), however, the third expanded row gets a value of 0 for column3 because (yeah, yeah, yeah, this isn't kindergarten math) it is not true that 2 < 2.

我可以在第一个给定的数据帧上使用什么函数,以获得类似于第二个的结果?

What function can I use on a dataframe like the one first given, to get a result that looks like the second one?

推荐答案

np.repeat + 总金额



np.repeat + cumcount

u = pd.DataFrame(np.repeat(df.values, df.column2, axis=0), columns=df.columns)

u.assign(
  column2=1,
  column3=(u.column3 > u.groupby('column1').cumcount()).astype(int)
)
  column1  column2  column3
0       a        1        1
1       a        1        1
2       b        1        0
3       c        1        1
4       c        1        1
5       c        1        0

这篇关于按列扩展行,同时根据值处理其他行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 16:48