Pandas : Python how to do index Groupby with replicates -



Pandas : Python how to do index Groupby with replicates -

i have dataframe similar this:

list1 = [4656, 5455, 4545, 6992, 4233, 4596, 4699, 4899, 7896, 4526, 4872, 6952] list2 = [4466, 4899, 4554, 4771, 1477, 1445, 4523, 1456, 3695, 6258, 1452, 4878] index1= ['a50_c1','a50_c2','a50_i1','a50_i2','a50_n1','a50_n2','a60_c1','a60_c2','a60_i1','a60_i2','a60_n1','a60_n2'] s1 = pd.series(list1, index=index1, name='list1') s2 = pd.series(list2, index=index1, name='list2') pd.concat([s1, s2], axis=1)

here looks like:

list1 list2 test a50_c1 4656 4466 a50_c2 5455 4899 a50_i1 4545 4554 a50_i2 6992 4771 a50_n1 4233 1477 a50_n2 4596 1445 a60_c1 4699 4523 a60_c2 4899 1456 a60_i1 7896 3695 a60_i2 4526 6258 a60_n1 4872 1452 a60_n2 6952 4878

i create groupby index (test column) i'm using : df2 = df1.groupby(df1.index) seems work grouping several time same row (probably create different combination possibles).

so can't figured out how groupby replicates in index ie: c1-c2 ; i1-i2 ; n1-n2

the result should this:

list1 list2 test a50_c1 4656 4466 a50_c2 5455 4899 list1 list2 test a50_i1 4545 4554 a50_i2 6992 4771 list1 list2 test a50_n1 4233 1477 a50_n2 4596 1445

any ideas ?

thanks in advance

the best approach add together column dataframe containing info want grouping on. each value in index single string; pandas can't guess parts of want grouping on, need explicitly extract parts relevant grouping.

based on example, looks want grouping on contents of index not including lastly character. create column:

df['label'] = df.index.to_series().str[:-1]

now can df.groupby('label') grouping on desired feature.

python pandas

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -