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

来源:互联网 发布:人工智能与自动控制 编辑:程序博客网 时间:2024/06/05 16:09

网址在此: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]}dic2type(dic2)df2 = pd.DataFrame(dic2)df2type(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}}dic3type(dic3)df3 = pd.DataFrame(dic3)df3type(df3)

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

df2Out[1]:    a  b   c   d0  1  5   9  131  2  6  10  142  3  7  11  153  4  8  12  16
dic3Out[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}}
df3Out[3]:    one  three  twoa    1      9    5b    2     10    6c    3     11    7d    4     12    8
原创粉丝点击