Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

concatenate the next row to the previous row pandas

'''
#Input
 doc  sent col1 col2 col3
0   0    0    5   4    8
1   0    1    6   3    2
2   0    2    1   2    9
3   1    0    6   1    6
4   1    1    5   1    5

#Required Output
   doc  sent  col1  col2  col3  ncol1  ncol2  ncol3  pcol1  pcol2  pcol3
0    0     0     5     4     8      6      3      2      0      0      0
1    0     1     6     3     2      1      2      9      5      4      8
2    0     2     1     2     9      0      0      0      6      3      2
3    1     0     6     1     6      5      1      5      0      0      0
4    1     1     5     1     5      0      0      0      6      1      6
'''
#Code
cs = ['col1', 'col2', 'col3']
g = df.groupby('doc')

pd.concat([
   df, 
   g[cs].shift(-1).add_prefix('n'), 
   g[cs].shift().add_prefix('p')
], axis=1).fillna(0).astype(int)
Comment

PREVIOUS NEXT
Code Example
Python :: add legend to px.choropleth map python 
Python :: pandas dtodays date csv 
Python :: sf.query_all( ) dataFrame records relation Id 
Python :: auto clipping path image using python 
Python :: integer to binary python 16 bit 
Python :: how to code a discord bot in python nextcord 
Python :: <function chr(i, /)> error in python 
Python :: const in python 3 
Python :: crop a video opencv 
Python :: geopandas change dtype of a columns 
Python :: how to convert array of arrays into single array with unique values in numpy 
Python :: pyjone location 
Python :: python dataset createdimension unlimited 
Python :: selecting letters in a row 
Python :: python default parameters depend on other paramters 
Python :: what should I do when the keras image datagenerato is nit working 
Python :: comment enleve les chiffre duplice d une liste python 
Python :: hackereath 
Python :: what is comma in regex 
Python :: qq plot using seaborn 
Python :: fibonacci sequence in python 2.7 
Python :: how to store svgs in django image field with SVGAndImageFormField 
Python :: boto3 cross region 
Python :: find-squares-and-odd-numbers-in-the-given-list 
Python :: How to make boxplot using seaborne 
Python :: effient way to find prime no inpython 
Python :: unpack 
Python :: !value in python 
Python :: how to run 2 async function forever 
Python :: pd.to_excel header char vertical 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =