Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeating elements in a dataframe

Hi all I have the following dataframe:

A | B | C
1   2   3 
2   3   4 
3   4   5
4   5   6

And I am trying to only repeat the last two rows of the data so that it looks like this:

A | B | C
1   2   3 
2   3   4 
3   4   5
3   4   5
4   5   6
4   5   6

I have tried using append, concat and repeat to no avail.

repeated = lambda x:x.repeat(2)
df.append(df[-2:].apply(repeated),ignore_index=True)

This returns the following dataframe, which is incorrect:

A | B | C
1   2   3 
2   3   4 
3   4   5
4   5   6
3   4   5
3   4   5
4   5   6
4   5   6
like image 491
codeninja Avatar asked Jan 22 '26 02:01

codeninja


2 Answers

You can use numpy.repeat for repeating index and then create df1 by loc, last append to original, but before filter out last 2 rows by iloc:

df1 = df.loc[np.repeat(df.index[-2:].values, 2)]
print (df1)
   A  B  C
2  3  4  5
2  3  4  5
3  4  5  6
3  4  5  6

print (df.iloc[:-2])
   A  B  C
0  1  2  3
1  2  3  4

df = df.iloc[:-2].append(df1,ignore_index=True)
print (df)
   A  B  C
0  1  2  3
1  2  3  4
2  3  4  5
3  3  4  5
4  4  5  6
5  4  5  6

If want use your code add iloc for filtering only last 2 rows:

repeated = lambda x:x.repeat(2)
df = df.iloc[:-2].append(df.iloc[-2:].apply(repeated),ignore_index=True)
print (df)
   A  B  C
0  1  2  3
1  2  3  4
2  3  4  5
3  3  4  5
4  4  5  6
5  4  5  6
like image 83
jezrael Avatar answered Jan 23 '26 17:01

jezrael


Use pd.concat and index slicing with .iloc:

pd.concat([df,df.iloc[-2:]]).sort_values(by='A')

Output:

   A  B  C
0  1  2  3
1  2  3  4
2  3  4  5
2  3  4  5
3  4  5  6
3  4  5  6
like image 43
Scott Boston Avatar answered Jan 23 '26 16:01

Scott Boston



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!