0

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!

Barmar
  • 741,623
  • 53
  • 500
  • 612
Gary
  • 2,137
  • 3
  • 23
  • 41
  • Can you show what the resulting df should look like? Are C and D columns, or are they rows? – Barmar Dec 11 '20 at 23:09
  • 1
    Does this answer your question? [Python Pandas ValueError Arrays Must be All Same Length](https://stackoverflow.com/questions/40442014/python-pandas-valueerror-arrays-must-be-all-same-length) – Pierre D Dec 11 '20 at 23:12
  • 1
    `pd.DataFrame.from_dict(my_dict_of_lists, orient='index').T.dropna()` as per https://stackoverflow.com/q/40442014/758174 – Pierre D Dec 11 '20 at 23:14

2 Answers2

1

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
Rajat Mishra
  • 3,635
  • 4
  • 27
  • 41
0

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()})
Barmar
  • 741,623
  • 53
  • 500
  • 612