I'm trying to convert a pandas dataframe into a dictionary but I need an specifyc ouput format, I have been reading and reviewing many other answers but I can't resolve; my dataframe looks like:
label Min Max Prom Desv. Est. Cr Tz Cpk Zup Zlow PPM % OOS # Datos
0 test1 1.25 1.46 1.329 0.0426 1.161 -0.023 0.697 2.090 3.077 19354 2 268
1 test2 4.80 5.50 5.110 0.1368 0.774 -1.097 0.926 2.778 4.972 2735 0 268
2 test3 2.58 2.96 2.747 0.0709 0.760 -1.029 0.973 2.918 4.977 1762 0 268
I've tried this (and others options but this is the most similar to the desire output):
dict = df.set_index('label').groupby('label').apply(lambda g: g.values.tolist()).to_dict()
And I got:
{'test1': [[1.25, 1.46, 1.329, 0.0426, 1.161, -0.023, 0.697, 2.09, 3.077, 19354.0, 2.0, 268.0]],
'test2': [[4.8, 5.5, 5.11, 0.1368, 0.774, -1.097, 0.926, 2.778, 4.972, 2735.0, 0.0, 268.0]],
'test3': [[2.58, 2.96, 2.747, 0.0709, 0.76, -1.0290, 0.973, 2.918, 4.977, 1762.0, 0.0, 268.0]]}
But what I'm looking for is something like:
{'label':'test1', 'cols':[1.25, 1.46, 1.329, 0.0426, 1.161, -0.023, 0.697, 2.09, 3.077, 19354.0, 2.0, 268.0]},
{'label':'test2', 'cols': [4.8, 5.5, 5.11, 0.1368, 0.774, -1.097, 0.926, 2.778, 4.972, 2735.0, 0.0, 268.0]},
{'label':'test3', 'cols': [2.58, 2.96, 2.747, 0.0709, 0.76, -1.0290, 0.973, 2.918, 4.977, 1762.0, 0.0, 268.0]}
Many thanks in advance for any idea or suggestion.
You can use a lambda function to build the output you want:
df.apply(lambda x: {'label':x.label, 'cols': x.tolist()[1:]}, axis=1).tolist()