python之pandas用法(用于矩阵等数据的操作)

网址在此:http://shujuren.org/article/207.html

下面是使用pandas生成DataFrame(数据框)的程序片段,使用pandas时一般要在前面添加:import pandas as pd

dic2 = {'a':[1,2,3,4],'b':[5,6,7,8],
        'c':[9,10,11,12],'d':[13,14,15,16]}
dic2
type(dic2)
df2 = pd.DataFrame(dic2)
df2
type(df2)
dic3 = {'one':{'a':1,'b':2,'c':3,'d':4},
        'two':{'a':5,'b':6,'c':7,'d':8},
        'three':{'a':9,'b':10,'c':11,'d':12}}
dic3
type(dic3)
df3 = pd.DataFrame(dic3)
df3
type(df3)

结果:dic2
Out[0]:
{'a': [1, 2, 3, 4],
'b': [5, 6, 7, 8],
'c': [9, 10, 11, 12],
'd': [13, 14, 15, 16]}

df2
Out[1]: 
   a  b   c   d
0  1  5   9  13
1  2  6  10  14
2  3  7  11  15
3  4  8  12  16
dic3
Out[2]: 
{'one': {'a': 1, 'b': 2, 'c': 3, 'd': 4},
 'three': {'a': 9, 'b': 10, 'c': 11, 'd': 12},
 'two': {'a': 5, 'b': 6, 'c': 7, 'd': 8}}
df3
Out[3]: 
   one  three  two
a    1      9    5
b    2     10    6
c    3     11    7
d    4     12    8

你可能感兴趣的:(python)