pandas的基础是numpy
- 两者可以试试相互转
- dataframe转化成numpy.ndarray
df=df.values
- numpy.ndarray转化成dataframe
df = pd.DataFrame(df)
关系
- DataFrame是Series的容器,Panel是DataFrame的容器 DataFrame用得最多
对比
pandas.Series( data, index, dtype, copy)
pandas.DataFrame( data, index, columns, dtype, copy)
pandas stack和unstack
-
图形直观说明
- 代码说明
import numpy as np import pandas as pd from pandas import Series,DataFrame data=DataFrame(np.arange(6).reshape((2,3)),index=pd.Index(['street1','street2']),columns=pd.Index(['one','two','three'])) print(data) print('-----------------------------------------\n') print(data.to_dict()) print('-----------------------------------------\n') data2=data.stack() data3=data2.unstack() print(data2) print('-----------------------------------------\n') print(data3)
-
返回结果
groupby使用
- 代码说明
df = pd.DataFrame({'key1':['a','a','b','b','a'],'key2':['one','two','one','two','one'],'data1':np.random.randn(5),'data2':np.random.randn(5)}) means = df['data1'].groupby([df['key1'], df['key2']]).sum() means.unstack()
-
返回