Reputation: 18810
I have two large dictionaries that has list has values. For this question purpose I name them as dictionary foo and bar.
foo = {}
foo['a'] = []
foo['b'] = []
foo['a'].append(1)
foo['a'].append(2)
foo['b'].append(10)
foo['b'].append(30)
bar = {}
bar['a'] = []
bar['a'].append(5)
bar['a'].append(7)
bar['b'] = []
bar['b'].append(8)
bar['b'].append(34)
bar['b'].append(32)
By iterating one dictionary I want to construct a data frame which will get me following data table:
id | viewed | presented
-----------------------
a | [1,2] | [5,7]
-----------------------
b | [10,30]| [8,32,34]
So I did the following:
import pandas as pd
df = pd.DataFrame(columns=['id', 'viewed', 'presented'])
for item in foo:
df = pd.Series({'id':item, 'viewed':foo[item], 'presented':bar[item]})
but when I print my data frame it was missing results of foo[a]
and bar[a]
>>> df
id b
presented [8, 34, 32]
viewed [10, 30]
dtype: object
>>> len(df.index)
3
Looks like it getting over written instead of increasing the index of the data frame to record the next element. What is the best way to solve this issue and insert row wise as I needed in this case. I looked at two other stackoverflow questions both didn't have the solution.
Upvotes: 0
Views: 881
Reputation: 5074
All you need to do is:
df = pd.DataFrame({'viewed':foo, 'presented':bar}).reset_index()
df.columns = ['id', 'viewed', 'presented']
And you will get the desired data frame of:
id viewed presented
0 a [1, 2] [5, 7]
1 b [10, 30] [8, 34, 32]
Upvotes: 2