Given this DataFrame
r3 value
r1 r2
1 2 3 1
2 4 1
3 2 1
3 4 1
4 2 1
4 3 1
2 1 3 1
1 4 1
3 1 1
3 4 1
4 1 1
4 3 1
...what's the best way to do this?
r3 value
r1 r2
1 2 3,4 2
3 2,4 2
4 2,3 2
2 1 3,4 2
3 1,4 2
4 1,3 2
Basically, I'm trying to condense the r3
column into a comma delimited string. The value
column can be achieved a different way later on if necessary, or if it can be done through this whole process, even better.
You could use the agg function after grouping the dataframe. if df
is your dataframe use this...
strJoin = lambda x:",".join(x.astype(str))
df.groupby(level=[0,1]).agg({"r3":strJoin,"value":np.sum})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With