2

from a data frame df1 =

Area Sequence X Y
A 2 604582.25 320710
A 1 604590.25 320704.75
A 3 604579.25 320710
B 2 536584.47 176977.83
B 1 536570 176996.43
C 1 509202.13 307995.99
C 2 509205.3 307951.24

Need to generate into

df1 =

Area XY_by_sequence
A 604590.25 320704.75 , 604582.25 320710 , 604579.25 320710
B 536570 176996.43 , 536584.47 176977.83
C 509202.13 307995.99 , 509205.3 307951.24

3 Answers3

7

Try:

x = (
    df.set_index(["Area", "Sequence"])
    .stack()
    .groupby(level=[0, 1])
    .agg(lambda x: " ".join(map(str, x)))
    .groupby(level=0)
    .agg(", ".join)
    .reset_index(name="XY_by_sequence")
)
print(x)

Prints:

  Area                                               XY_by_sequence
0    A  604590.25 320704.75, 604582.25 320710.0, 604579.25 320710.0
1    B                      536570.0 176996.43, 536584.47 176977.83
2    C                      509202.13 307995.99, 509205.3 307951.24
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
2

Another possible solution:

(df.sort_values(['Area', 'Sequence'])
 .assign(XY=df['X'].astype('str') + ' ' + df['Y'].astype('str'))
 .groupby('Area')['XY'].agg(lambda g: ', '.join(g))
 .reset_index())

Output:

  Area                                                 XY
0    A  604590.25 320704.75, 604582.25 320710.0, 60457...
1    B            536570.0 176996.43, 536584.47 176977.83
2    C            509202.13 307995.99, 509205.3 307951.24
PaulS
  • 21,159
  • 2
  • 9
  • 26
1

One way of doing (if you know how many groups you'll have), is creating N groups, and defining their start and end. Then, you'd have to create a new table where you'd add a row each time the value in Area changes. Another way is simply this :

import pandas

value = df['Area'].iloc[0]

string a =""
#generate new table with a first blank row
for row in df.iterrows():
    if(row['Area']==value):
        a+ = row['X']+" "+row['Y']+","
        #add a to your newly created table in the row you generated
        a = ""
    else :
        value= row['Area']
        #generate new row
        a+ = row['X']+" "+row['Y']+","
        #add a to your newly created table in the row you generated
        a = ""
        

N.B : The comments are for things you'll have to do yourself