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
>>new=raw.iloc [raw.index.repeat(raw['num')]
>>>new
name num
0 a 1
1 b 2
1 b 2
2c3
2c3
2c3
© 2025 OneMinuteCode. All rights reserved.