assigning dataframe to Panel in Pandas

泄露秘密 提交于 2019-12-13 16:16:48

问题


I intend to read from a directory a list of CSV files and assign them to a Panel as a series of dataframes. But the Panel doesn't seem to taking the return from the read_csv(). I am using Pandas 0.11 and NumPy 1.7.1. Here's a snippet of what I have done so far:

dp = pd.Panel({})    
j = 0
for csv in csv_list:
    dp[j] = pd.read_csv(csv, index_col=key, sep=delim_list[j])
    j += 1

But from idle it shows that:

>>> dp[0]
Empty DataFrame
Columns: []
Index: []

What am I doing wrong?


回答1:


Make sure that your csv is reading in correctly

make dp a dict, then create the Panel at the end

In [1]: p = pd.Panel(dict([ (i,DataFrame(randn(3,2))) for i in range(4) ]))

In [2]: p[0]
Out[2]: 
          0         1
0 -1.400715 -1.283452
1 -0.237714  0.677903
2  0.295015 -0.536786

In [3]: p
Out[3]: 
<class 'pandas.core.panel.Panel'>
Dimensions: 4 (items) x 3 (major_axis) x 2 (minor_axis)
Items axis: 0 to 3
Major_axis axis: 0 to 2
Minor_axis axis: 0 to 1


来源:https://stackoverflow.com/questions/17243186/assigning-dataframe-to-panel-in-pandas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!