I have a list of dicts where each dict can have different keys. I want to create a dataframe with one row where each key is a column and the row is its value:
import pandas as pd
data = [{"A":1}, {"B":2}, {"C":3}]
df = pd.DataFrame(data)
print(df.to_string(index=False))
# A B C
# 1.0 NaN NaN
# NaN 2.0 NaN
# NaN NaN 3.0
What I want:
# A B C
# 1.0 2.0 3.0
How can I drop/compact the rows with NaN values?
One option would be to stack
:
df.stack().droplevel(0).to_frame().T
Or using a dummy groupby
:
import numpy as np
df.groupby(np.repeat(0, len(df))).first()
Output:
A B C
0 1.0 2.0 3.0