a method of replicating a row based on the value of a particular column in pandas

Asked 1 years ago, Updated 1 years ago, 284 views

pandas.DataFrame is looking for a way to duplicate rows for the number of values in a particular column.

The source code shown below replicates the rows so that they have the same number as the values in the num column.
But this is probably the least efficient way. Please let me know if there is an efficient way.

It is guaranteed that the data frame before operation does not contain the same line.

Current State Code:

#Python 3.10.5

import pandas aspd

raw=pd.DataFrame([['a',1],['b',2],['c',3]],columns=['name','num'])
new=pd.DataFrame(columns=raw.columns)

for_, row in raw.iterrows():
    for_in range (row.num):
        new=new.append(row)

Data frames before operation:

name num
0 a 1
1 b 2
2c3

Data frames after operation:

name num
0 a 1
1 b 2
1 b 2
2c3
2c3
2c3

python python3 pandas

2022-11-09 19:06

1 Answers

>>new=raw.iloc [raw.index.repeat(raw['num')]
>>>new
  name num
0 a 1
1 b 2
1 b 2
2c3
2c3
2c3


2022-11-09 23:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.