简体   繁体   中英

Duplicating rows in a DataFrame based on column value

Below is a set of sample data I am working with:

sample_dat = pd.DataFrame(
    np.array([[1,0,1,1,1,5],
              [0,0,0,0,1,3],
              [1,0,0,0,1,1],
              [1,0,0,1,1,1],
              [1,0,0,0,1,1],
              [1,1,0,0,1,1]]),
    columns=['var1','var2','var3','var4','var5','cnt']
)

I need to change the data so the rows are duplicated according to the value in the last column. Specifically I wish for it to do be duplicated based on the value in the cnt column.

My search yielded lots of stuff about melts, splits, and other stuff. I think what I am looking for is very basic, hopefully. Please also note that I will likely have some kind of an id in the first column that will be either an integer or string.

For example, the first record will be duplicated 4 more times. The second record will be duplicated twice more.

An example of what the DataFrame would look like if I were manually doing it with syntax is below:

sample_dat2 = pd.DataFrame(
    np.array([[1,0,1,1,1,5],
              [1,0,1,1,1,5],
              [1,0,1,1,1,5],
              [1,0,1,1,1,5],
              [1,0,1,1,1,5],
              [0,0,0,0,1,3],
              [0,0,0,0,1,3],
              [0,0,0,0,1,3],
              [1,0,0,0,1,1],
              [1,0,0,1,1,1],
              [1,0,0,0,1,1],
              [1,1,0,0,1,1]]),
    columns=['var1','var2','var3','var4','var5','cnt']
)

Create an empty dataframe then iterate over your data, appending each row to the new dataframe x amount of times where x is the number in the 'cnt' column.

df =pd.DataFrame()

for index, row in sample_dat.iterrows():
    for x in range(row['cnt']):
        df = df.append(row, ignore_index=True)

Output

>>> df
   cnt  var1  var2  var3  var4  var5
0  5.0   1.0   0.0   1.0   1.0   1.0
0  5.0   1.0   0.0   1.0   1.0   1.0
0  5.0   1.0   0.0   1.0   1.0   1.0
0  5.0   1.0   0.0   1.0   1.0   1.0
0  5.0   1.0   0.0   1.0   1.0   1.0
1  3.0   0.0   0.0   0.0   0.0   1.0
1  3.0   0.0   0.0   0.0   0.0   1.0
1  3.0   0.0   0.0   0.0   0.0   1.0
2  1.0   1.0   0.0   0.0   0.0   1.0
3  1.0   1.0   0.0   0.0   1.0   1.0
4  1.0   1.0   0.0   0.0   0.0   1.0
5  1.0   1.0   1.0   0.0   0.0   1.0

I will use numpy repeat based on the dataframe index location. Then reset the index.

sample_dat.loc[numpy.repeat(sample_dat.index.values, sample_dat.cnt)].reset_index(drop=True)

Result:

   var1 var2 var3 var4 var5 cnt
0      1    0   1   1   1   5
1      1    0   1   1   1   5
2      1    0   1   1   1   5
3      1    0   1   1   1   5
4      1    0   1   1   1   5
5      0    0   0   0   1   3
6      0    0   0   0   1   3
7      0    0   0   0   1   3
8      1    0   0   0   1   1
9      1    0   0   1   1   1
10     1    0   0   0   1   1
11     1    1   0   0   1   1

You can use numpy.repeat along with indexing to return an array of values from the column that determines the number of repetitions.

import numpy as np
import pandas as pd

arr = np.array(
    [[1,0,1,1,1,5],
     [0,0,0,0,1,3],
     [1,0,0,0,1,1],
     [1,0,0,1,1,1],
     [1,0,0,0,1,1],
     [1,1,0,0,1,1]]
    )

df = pd.DataFrame(
    np.repeat(arr, arr[:,5], axis=0),
    columns=['var1','var2','var3','var4','var5','cnt']
    )

print(df)
#     var1  var2  var3  var4  var5  cnt
# 0      1     0     1     1     1    5
# 1      1     0     1     1     1    5
# 2      1     0     1     1     1    5
# 3      1     0     1     1     1    5
# 4      1     0     1     1     1    5
# 5      0     0     0     0     1    3
# 6      0     0     0     0     1    3
# 7      0     0     0     0     1    3
# 8      1     0     0     0     1    1
# 9      1     0     0     1     1    1
# 10     1     0     0     0     1    1
# 11     1     1     0     0     1    1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM