Gary
Gary

Reputation: 2167

How to convert a dict of lists into a dataframe; choosing specific index

I have a dict of lists with different lengths:

my_dict_of_lists = {'D': [79,67,46], 'C': [25, 56, 76, 45, 54, 67, 98, 45]}

I would like to create a dataframe taking only the first three datapoints from 'C'; but I'm struggling on how to index these out.

my_new_df  =    pd.DataFrame.from_dict(my_dict_of_lists)

Ultimately, I want my dataframe to have the 3 items in 'D' and the first 3 items in 'C'.

Thanks for your help!

Upvotes: 0

Views: 207

Answers (2)

Barmar
Barmar

Reputation: 781004

Use a dictionary comprehension to slice the first three elements of each dictionary value.

my_new_df = pd.DataFrame.from_dict({key: value[:3] for key, value in my_dict_of_lists.items()})

Upvotes: 0

Rajat Mishra
Rajat Mishra

Reputation: 3770

you can try creating dataframe and drop the rows where the column D has NaN values.

In [55]: df = pd.DataFrame(dict([ (k,pd.Series(v)) for k,v in my_dict_of_lists.items() ]))                                                                                      

In [56]: df                                                                                                                                                                     
Out[56]: 
      D   C
0  79.0  25
1  67.0  56
2  46.0  76
3   NaN  45
4   NaN  54
5   NaN  67
6   NaN  98
7   NaN  45

In [57]: df.dropna(inplace=True)                                                                                                                                                

In [58]: df                                                                                                                                                                     
Out[58]: 
      D   C
0  79.0  25
1  67.0  56
2  46.0  76

Upvotes: 1

Related Questions