I have a Pandas dataframe that looks like this:
df = pd.DataFrame({'c1': [10, 20, 30, 40, 50,60,70],
'c2': [[(0.14507, 0.163054, 0.330704), (0.069578, 0.075506, 0.145531), (0.039168, 0.042362, 0.097616), (0.038133, 0.04009, 0.079482), (0.039872, 0.041051, 0.06478), (0.039158, 0.039898, 0.054787), (0.03744, 0.037917, 0.047526), (0.035354, 0.035669, 0.041999), (0.033199, 0.03341, 0.037646), (0.031113, 0.031256, 0.034127), (0.02916, 0.029257, 0.031222), (0.027361, 0.027428, 0.028784), (0.02572, 0.025767, 0.026709)],
np.nan,
[(0.000341, 0.000391, 0.000991), (0.006122, 0.006682, 0.013834), (0.008017, 0.008671, 0.01998), (0.011591, 0.012186, 0.02416), (0.016047, 0.016522, 0.026072), (0.019587, 0.019957, 0.027405), (0.022358, 0.022643, 0.028382), (0.024514, 0.024733, 0.029122), (0.02619, 0.026356, 0.029698), (0.027492, 0.027618, 0.030155), (0.028507, 0.028602, 0.030523), (0.0293, 0.029373, 0.030825), (0.029924, 0.029979, 0.031075)],
[(0.000341, 0.000391, 0.000991), (0.006122, 0.006682, 0.013834), (0.008017, 0.008671, 0.01998), (0.011591, 0.012186, 0.02416), (0.016047, 0.016522, 0.026072), (0.019587, 0.019957, 0.027405), (0.022358, 0.022643, 0.028382), (0.024514, 0.024733, 0.029122), (0.02619, 0.026356, 0.029698), (0.027492, 0.027618, 0.030155), (0.028507, 0.028602, 0.030523), (0.0293, 0.029373, 0.030825), (0.029924, 0.029979, 0.031075)],
[(0.041119, 0.046924, 0.111008), (0.027748, 0.030228, 0.061034), (0.019769, 0.02138, 0.049267), (0.021487, 0.02259, 0.044787), (0.024792, 0.025526, 0.04028), (0.026614, 0.027117, 0.037236), (0.027595, 0.027947, 0.03503), (0.028072, 0.028322, 0.033348), (0.028237, 0.028416, 0.03202), (0.028208, 0.028337, 0.03094), (0.02806, 0.028153, 0.030044), (0.02784, 0.027908, 0.029288), (0.02758, 0.02763, 0.02864)],
[(5.25, 6.16, 7.7)],
[(0.003174, 0.003642, 0.009186), (0.007794, 0.008506, 0.017575), (0.009007, 0.009741, 0.022447), (0.012455, 0.013094, 0.025961), (0.016847, 0.017345, 0.027372), (0.020272, 0.020655, 0.028363), (0.022917, 0.023209, 0.029091), (0.024952, 0.025175, 0.029642), (0.026517, 0.026685, 0.03007), (0.027721, 0.027848, 0.030406), (0.028649, 0.028745, 0.030676), (0.029367, 0.02944, 0.030895), (0.029924, 0.029979, 0.031075)]]})´´´
I want to split all the tuples in different columns and then unpack the tuples in columns with the numbers. Something like this:
How can I do that?
Thank you!
CodePudding user response:
If I understand you correctly, you want to expand the inner values in the tuples into separate columns:
x = df["c2"].explode().explode().groupby(level=0).agg(list).apply(pd.Series)
x.columns = [f"c{c 3}" for c in x.columns]
print(pd.concat([df["c1"], x], axis=1))
Prints:
c1 c3 c4 c5 c6 c7 c8 c9 c10 c11 c12 c13 c14 c15 c16 c17 c18 c19 c20 c21 c22 c23 c24 c25 c26 c27 c28 c29 c30 c31 c32 c33 c34 c35 c36 c37 c38 c39 c40 c41
0 10 0.145070 0.163054 0.330704 0.069578 0.075506 0.145531 0.039168 0.042362 0.097616 0.038133 0.040090 0.079482 0.039872 0.041051 0.064780 0.039158 0.039898 0.054787 0.037440 0.037917 0.047526 0.035354 0.035669 0.041999 0.033199 0.033410 0.037646 0.031113 0.031256 0.034127 0.029160 0.029257 0.031222 0.027361 0.027428 0.028784 0.025720 0.025767 0.026709
1 20 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
2 30 0.000341 0.000391 0.000991 0.006122 0.006682 0.013834 0.008017 0.008671 0.019980 0.011591 0.012186 0.024160 0.016047 0.016522 0.026072 0.019587 0.019957 0.027405 0.022358 0.022643 0.028382 0.024514 0.024733 0.029122 0.026190 0.026356 0.029698 0.027492 0.027618 0.030155 0.028507 0.028602 0.030523 0.029300 0.029373 0.030825 0.029924 0.029979 0.031075
3 40 0.000341 0.000391 0.000991 0.006122 0.006682 0.013834 0.008017 0.008671 0.019980 0.011591 0.012186 0.024160 0.016047 0.016522 0.026072 0.019587 0.019957 0.027405 0.022358 0.022643 0.028382 0.024514 0.024733 0.029122 0.026190 0.026356 0.029698 0.027492 0.027618 0.030155 0.028507 0.028602 0.030523 0.029300 0.029373 0.030825 0.029924 0.029979 0.031075
4 50 0.041119 0.046924 0.111008 0.027748 0.030228 0.061034 0.019769 0.021380 0.049267 0.021487 0.022590 0.044787 0.024792 0.025526 0.040280 0.026614 0.027117 0.037236 0.027595 0.027947 0.035030 0.028072 0.028322 0.033348 0.028237 0.028416 0.032020 0.028208 0.028337 0.030940 0.028060 0.028153 0.030044 0.027840 0.027908 0.029288 0.027580 0.027630 0.028640
5 60 5.250000 6.160000 7.700000 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
6 70 0.003174 0.003642 0.009186 0.007794 0.008506 0.017575 0.009007 0.009741 0.022447 0.012455 0.013094 0.025961 0.016847 0.017345 0.027372 0.020272 0.020655 0.028363 0.022917 0.023209 0.029091 0.024952 0.025175 0.029642 0.026517 0.026685 0.030070 0.027721 0.027848 0.030406 0.028649 0.028745 0.030676 0.029367 0.029440 0.030895 0.029924 0.029979 0.031075