Sorry late to the party. Variation to original requirement. How to capture sas first dot records with python program? Below sample is based on https://pandas.pydata.org/pandas-docs/stable/getting_started/comparison/comparison_with_sas.html
First sas setup:
the sample_dot_last and sample_dot_first datasets are what I need python to produce!
data sampledata;
infile cards4;
input ( x y ) ( 2*$8. ) z record_number;
cards;
A I 10 1
A I 11 2
A I 11 3
A J 15 4
B K 9 5
B K 10 6
B K 10 7
B L 14 8
C I 7 9
C I 19 10
C K 3 11
C K 5 12
;;;;
proc print data= sampledata;
run;
data sample_dot_last;
set sampledata;
by x y z;
if last.y;
run;
proc print data= sample_dot_last;
run;
data sample_dot_first;
set sampledata;
by x y z;
if first.y;
run;
proc print data= sample_dot_first;
run;
Second sample csv for python:
x,y,z,record number
A,I,10,1
A,I,11,2
A,I,11,3
A,J,15,4
B,K,9,5
B,K,10,6
B,K,10,7
B,L,14,8
C,I,7,9
C,I,19,10
C,K,3,11
C,K,5,12
Finally python program, note the dataframe.groupby( [ ... ] ).last() or .first()
produces exact same output as sas!
import numpy as np
import pandas as pd
import os
cwd= os.getcwd()
print( "cwd={}".format( cwd ))
df1= pd.read_csv( 'sampledata.csv')
print( df1 )
df2= df1.groupby( [ 'x', 'y' ]).last()
print( df2 )
df3= df1.groupby( [ 'x', 'y' ]).first()
print( df3 )
Sorry different question and answer, hope it useful.