3

I have a dictionary of 68 keys whereby each key has a list with 50 values in it. For example, my dict is the following, whereby each series has 50 values in it, e.g. value1, value2....

key1 : Series1
key2 : Series2
 .   :  .
key50:  Series50

I now want to make the following dataframe out of the dictionary:

key1          key2
value1      value1
 .            .
 .            .
value50     value 50

I looked at other threads and tried the following command:

df= pd.DataFrame([dict])

However, this yields:

key1          key2
Series1       Series2

How doI get the values in the dataframe instead of the Series. In the end, I should get dataframe sized 50*68.

4 Answers 4

7

Just pass dict_ directly:

df = pd.DataFrame(dict_)

Also, don't use dict as a variable name, it's bad form, and it shadows the builtin class with the same name.

Sign up to request clarification or add additional context in comments.

7 Comments

Sometimes I think too much. Unpacking a single element list is nothing but passing it directly ;).
@Bharathshetty I thought to correct you, but I was itching to write an answer.. :-)
Nowadays very less pandas questions.
thank you guys. I realized I had different indices for the several series. I resetted the indices and then it worked like a charm. :) Thanks!
it shadows the builtin this will leads to 'dict' object is not callable. I faced it a lot of times.
|
1

unpacking the list might help i.e

df = pd.DataFrame(*[dict])

If you have dict of series like

df = pd.DataFrame({'A':[1,2,3,4,5,6],'B':[2,5,1,1,5,1]})
data = {'A' : df['A'], 'B' : df['B']}

Then

ndf = pd.DataFrame(*[data])
   A  B
0  1  2
1  2  5
2  3  1
3  4  1
4  5  5
5  6  1

Comments

0

I just struggled with something similar, I have a dictionary of pd.Series. I want the dictionary keys to be the index and the series index to be the column names:

dic ={0: pd.Series([0.1,0.2,0.3],index=['a','b','c']), 1: pd.Series([1.1,1.2,1.3], index=['a','b','c'])}

dic

Out[21]: 
{0: a    0.1
 b    0.2
 c    0.3
 dtype: float64,
 1: a    1.1
 b    1.2
 c    1.3
 dtype: float64}

df = pd.DataFrame(dic).T

df

Out[23]: 
     a    b    c
0  0.1  0.2  0.3
1  1.1  1.2  1.3

I think the solution to your problem is just without the .T?

df = pd.DataFrame(dic)
df
Out[4]: 
     0    1
a  0.1  1.1
b  0.2  1.2
c  0.3  1.3

Comments

0
import pandas as pd

mydict={'A':[1,2,3,4,5,6],'B':[2,5,1,1,5,1]}
df = pd.DataFrame(*[mydict])
df.head()

Output:

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.