Oblomov
Oblomov

Reputation: 9635

Problems extending pandas Panels dynamically, DataFrame by DataFrame

I want to construct a pandas panel dynamically, using the following - simplified - code:

import pandas as pd 
rows=range(0,3)
pl=pd.Panel()
for i in range(0,3):
    pl[i]=pd.DataFrame()
    for j in rows:
        pl[i].set_value(j,'test_value','test')

Which seems to work fine. But when I the try to index the individual dataframes by

for i in range(0,3):
    print pl[i]

I get the output

Empty DataFrame
Columns: []
Index: []
Empty DataFrame
Columns: []
Index: []
  test_value
0     test20
1     test21
2     test22

Why are the first two frames empty?

Upvotes: 0

Views: 58

Answers (1)

Sayali Sonawane
Sayali Sonawane

Reputation: 12599

Use two for loops to solve this problem:

import pandas as pd 

rows=range(0,3)
pl=pd.Panel()

#first for loop to create dataframe
for i in range(0,3):
    pl[i]=pd.DataFrame()

#Second for loop to assign values
for i in range(0,3):    
    for j in rows:
        pl[i].set_value(j,'test_value','test')

This time you won't get empty dataframes :)

Upvotes: 1

Related Questions