Reputation: 32286
I need to convert the first row to a dictionary and then remove all "nan" values. This works...
pd.Series(df.head(1).T.to_dict()[0]).dropna()
Or. this one...
df.head(1).dropna(axis=1).T.to_dict()[0]
But is there any other way to achive this? The type of dataframe is sparse, if that matters.
type(df)
pandas.core.sparse.frame.SparseDataFrame
I am asking because I do not think it is pythonic and I guess there must be a method to do this.
Upvotes: 0
Views: 48
Reputation: 28303
df.iloc[0].dropna().to_dict()
is shorter and does not require any transposing.
the .iloc
method returns a series when provided a single row index, which is then cleansed of null-values & converted to a dictionary.
Upvotes: 1