首页 > 编程 > Python > 正文

python基础数据分析--pandas(一)

2019-11-06 08:06:36
字体:
来源:转载
供稿:网友

本文为原创,未经允许,不得转载。

1、pandas的数据结构

1)Series:是一种类似于一维数组的对象,它由一组数据(各种NumPy数据类型)以及一组与之相关的数据标签(即索引)组成。它可以看做一个定长的有序字典。基本任意的一维数据都可以用来构造 Series 对象。

2)DataFrame:是一个表格型的数据结构,它含有一组有序的列,每列可以是不同的值类型(数值、字符串、布尔值的)。DataFrame既有行索引也有列索引,它可以被看做由Series组成的字典。

Series 和 DataFrame 分别对应于numpy一维的序列和二维的表结构。

2. pandas的数据结构DataFrame是一个表格型的数据结构,它含有一组有序的列,每列可以是不同的值类型(数值、字符串、布尔值的)。

下面具体练习:

import pandas as pd

import numpy as np

#Series 对象包含两个主要的属性:index 和 values

w =pd.Series(data=[1,3,5,7],index = ['a','b','c','d'])

PRint (w)
a    1

b    3

c    5

d    7

dtype: int64

w.index

Index(['a', 'b', 'c', 'd'], dtype='object')

w.values

array([1, 3, 5, 7], dtype=int64)

s = pd.Series([1,3,5,np.nan,6,8])

print(s)

0    1.0

1    3.0

2    5.0

3    NaN

4    6.0

5    8.0

dtype: float64

##非常常用的函数date_range,尤其是在处理时间序列数据时,这个函数的作用就是产生一个##DatetimeIndex,就是时间序列数据的索引

dates = pd.date_range('20130101', periods=6) 

# periods:表示你要从这个函数产生多少个日期索引值

dates 

DatetimeIndex(['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04',

'2013-01-05', '2013-01-06'],

dtype='datetime64[ns]', freq='D')

df = pd.DataFrame(np.random.randn(6,4), index=dates, columns=list('ABCD'))

print(df)

 ABCD
2013-01-01-0.747037-0.6659330.506150-0.047263
2013-01-02-0.5010750.8436470.506725-0.499766
2013-01-03-1.1714450.8164340.761552-0.908022
2013-01-04-0.226046-0.4727653.0757161.063544
2013-01-050.108281-0.122184-0.011140-1.287596
2013-01-06-1.499848-0.4302672.074067-1.067155

df1 = pd.DataFrame(np.random.randn(6,4), index=['a','b','c','d','e','f'], columns=list('ABCD'))

df1

Out[14]:

 ABCD
a-1.6369000.9667560.742416-1.163262
b0.035832-0.9605780.2531730.165655
c-1.0324650.2701601.4454540.598161
d2.112374-0.890787-0.308986-1.187998
e-1.834906-1.9398690.924308-1.150917
f0.1485562.4105710.083520-2.209039

df.head()

Out[9]:

 ABCD
2013-01-01-0.5428352.6400711.2442491.613720
2013-01-020.2702100.238108-1.111091-0.700783
2013-01-03-0.681126-1.618220-0.2215470.379695
2013-01-04-0.490011-0.3015521.951446-0.074140
2013-01-05-0.5499300.558058-0.0150880.092985

df.tail() #默认是5行

Out[11]:

 ABCD
2013-01-020.2702100.238108-1.111091-0.700783
2013-01-03-0.681126-1.618220-0.2215470.379695
2013-01-04-0.490011-0.3015521.951446-0.074140
2013-01-05-0.5499300.558058-0.0150880.092985
2013-01-060.761844-0.9381380.065372-0.677193

df.tail(3) #查看后3行

Out[12]:

 ABCD
2013-01-04-0.490011-0.3015521.951446-0.074140
2013-01-05-0.5499300.558058-0.0150880.092985
2013-01-060.761844-0.9381380.065372-0.677193

df.columns#df的列名

Out[13]:

Index(['A', 'B', 'C', 'D'], dtype='object')

df.describe()#统计性描述分析

Out[14]:

 ABCD
count6.0000006.0000006.0000006.000000
mean-0.2053080.0963880.3188900.105714
std0.5833781.4745871.0985680.853820
min-0.681126-1.618220-1.111091-0.700783
25%-0.548156-0.778992-0.169932-0.526430
50%-0.516423-0.0317220.0251420.009422
75%0.0801550.4780710.9495300.308018
max0.7618442.6400711.9514461.613720

df.T#转置

 2013-01-01 00:00:002013-01-02 00:00:002013-01-03 00:00:002013-01-04 00:00:002013-01-05 00:00:002013-01-06 00:00:00
A-0.5428350.270210-0.681126-0.490011-0.5499300.761844
B2.6400710.238108-1.618220-0.3015520.558058-0.938138
C1.244249-1.111091-0.2215471.951446-0.0150880.065372
D1.613720-0.7007830.379695-0.0741400.092985-0.677193

df.sort_index(axis=1, ascending=False) #以列降序排列

DCBA 
2013-01-011.6137201.2442492.640071-0.542835
2013-01-02-0.700783-1.1110910.2381080.270210
2013-01-030.379695-0.221547-1.618220-0.681126
2013-01-04-0.0741401.951446-0.301552-0.490011
2013-01-050.092985-0.0150880.558058-0.549930
2013-01-06-0.6771930.065372-0.9381380.761844

df.sort_values(by='B') ##按B列的值来排序,默认是升序

ABCD 
2013-01-03-0.681126-1.618220-0.2215470.379695
2013-01-060.761844-0.9381380.065372-0.677193
2013-01-04-0.490011-0.3015521.951446-0.074140
2013-01-020.2702100.238108-1.111091-0.700783
2013-01-05-0.5499300.558058-0.0150880.092985
2013-01-01-0.5428352.6400711.2442491.613720

后续会持续更新,欢迎大家进群一起学习,一起沟通。

1850000767f484c9d6d3

数据分析群


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表