如果我创建一个DataFrame,其中一列是一个对象,另一列是一个整数,则将该DataFrame放入字典中,然后使用该字典创建Panel,则整数数据类型将消失:import pandas as pdimport datetime as dta=['a' for i in range(1000)]b=[i for i in range(1000)]c = {'c':a[:], 'b':b[:]}df = pd.DataFrame(c)print dfd = dict()d['x'] = dfp = pd.Panel(d)print p['x']输出:<class 'pandas.core.frame.DataFrame'>Int64Index: 1000 entries, 0 to 999Data columns (total 2 columns):b 1000 non-null valuesc 1000 non-null valuesdtypes: int64(1), object(1)<class 'pandas.core.frame.DataFrame'>Int64Index: 1000 entries, 0 to 999Data columns (total 2 columns):b 1000 non-null valuesc 1000 non-null valuesdtypes: object(2)从包含DataFrame的字典创建Panel时,是否可以保留数据类型?
2 回答
回首忆惘然
TA贡献1847条经验 获得超11个赞
它们需要位于单独的框架中,以避免组合类型。这是一种方法
In [52]: df = pd.DataFrame(c)
In [53]: df
Out[53]:
<class 'pandas.core.frame.DataFrame'>
Int64Index: 1000 entries, 0 to 999
Data columns (total 2 columns):
b 1000 non-null values
c 1000 non-null values
dtypes: int64(1), object(1)
In [54]: df['x'] = 'x'
In [55]: df['y'] = range(len(df))
In [56]: p = df.set_index(['x','y']).to_panel()
In [10]: p['b']
Out[10]:
<class 'pandas.core.frame.DataFrame'>
Index: 1 entries, x to x
Columns: 1000 entries, 0 to 999
dtypes: int64(1000)
In [11]: p['c']
Out[11]:
<class 'pandas.core.frame.DataFrame'>
Index: 1 entries, x to x
Columns: 1000 entries, 0 to 999
dtypes: object(1000)
添加回答
举报
0/150
提交
取消