码迷,mamicode.com
首页 > 其他好文 > 详细

pandas

时间:2019-03-02 17:18:47      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:com   默认   dex   code   orange   app   and   ide   das   

pandas基础 Series DataFrame

技术图片
 1 import pandas as pd
 2 import numpy as np
 3 
 4 # 创建Series 索引为默认值
 5 a = pd.Series([1, 2, 3, 4])
 6 print(a)
 7 print(a.values)
 8 print(a.index)
 9 # 自己设置索引
10 b = pd.Series([5, 6, 7, 8, 9], index=[‘a‘, ‘b‘, ‘d‘, ‘f‘, ‘888‘])
11 print(b)
12 print(b.values)
13 print(b.index)
14 # 根据索引取值
15 print(b[‘f‘])
16 print(b[[‘d‘, ‘f‘, ‘888‘]])
17 # 判断索引是否存在 不是判断元素
18 print(‘a‘ in b)
19 print(9 in b)
20 
21 # Series可以看成是一个定长的有序字典
22 dic1 = {‘apple‘: 5, ‘pen‘: 3, ‘orange‘: 8}
23 c = pd.Series(dic1)
24 print(c)
25 
26 # DataFrame
27 data = {‘year‘: [2015, 2016, 2017, 2018], ‘income‘: [10000, 30000, 50000, 80000], ‘pay‘: [3333, 8888, 6666, 9999]}
28 df1 = pd.DataFrame(data)
29 print(df1)
30 print(df1.columns)
31 print(df1.index)
32 print(df1.values)
33 
34 
35 df2 = pd.DataFrame(np.arange(12).reshape((3, 4)))
36 print(df2)
37 
38 df3 = pd.DataFrame(np.arange(12).reshape((3, 4)), index=[‘a‘, ‘c‘, ‘b‘], columns=[11, 22, 33, 44])
39 print(df3)
40 print(df3.columns)
41 print(df3.index)
42 print(df3.values)
43 # 描述
44 print(df3.describe())
45 # 转置
46 print(df3.T)
47 # 排序 按列排序
48 print(df3.sort_index(axis=1))
49 # 排序 按行排序
50 print(df3.sort_index(axis=0))
51 # 对某一列元素进行排序
52 print(df3.sort_values(by=44))
View Code

 

pandas

标签:com   默认   dex   code   orange   app   and   ide   das   

原文地址:https://www.cnblogs.com/sniperths/p/10461834.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!