3

I want to pass a dictionary of items into Dataframe, but these items can have variable length arrays. What is the most elegant way to append np.nan to the arrays so that they match the sizes? For example:

pd.DataFrame.from_dict({"a":[1,2,3],"b":[1,2], "c":[1]}) 

should return me

a b c 1 1 1 2 2 nan 3 nan nan 

2 Answers 2

5

maybe:

d = {"a":[1,2,3],"b":[1,2], "c":[1]} pd.concat(map(pd.Series, d.values()), keys=d.keys(), axis=1) 
Sign up to request clarification or add additional context in comments.

Comments

1

Alternate solution:

d = {"a":[1,2,3],"b":[1,2], "c":[1]} pd.DataFrame({x:pd.Series(d[x]) for x in d}) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.