Analyte_line
0 NaN
1 [['Urea', 3.0, '3', ''], ['Creatinine', 3.0, '...
2 [['Total Protein', '', '6', ''], ['Albumin', '...
3 [['HGB', '', '18', ''], ['RBC', '', '1', ''], ...
4 [['Total Protein', '', '23', ''], ['Albumin', ...
.. ...
102 [['Rapid Malaria', '', 'NEGATIVE', '']]
103 [['Rapid Malaria', '', 'POSITIVE (P.VIVAX)', '']]
104 [['Rapid Malaria', '', 'NEGATIVE', '']]
105 [['Rapid Malaria', '', 'POSITIVE (P.VIVAX)', '']]
106 [['Rapid Malaria', '', 'NEGATIVE', '']]
this given data is stored in a data frame.
df = pd.DataFrame(data)
print(df)
In data each name and value is stored list. How can we convert this data frame in row and columns wise like
Urea | Creatinine | Uric Acid | Alkaline Phosphatase | Test |
---|---|---|---|---|
3.0 , 3 | 3.0 , 3 | 3.0 , 3 | 4 | Positive |
CodePudding user response:
You can use:
out = (df
.set_index(0).T.astype(str)
.agg(lambda s: ','.join(x for x in s if x))
.to_frame().T
)
Or:
out = pd.DataFrame.from_dict({0: {k: ','.join(x for x in map(str, l) if x)
for k, *l in data}}, orient='index')
Output:
Urea Creatinine Uric Acid Alkaline Phosphatase Test
0 3.0,3 3.0,3 3.0,3 4 Positive