pandas基础,Serires,Dataframe

DataFrame

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

Series

它是一种类似于一维数组的对象,是由一组数据(各种NumPy数据类型)以及一组与之相关的数据标签(即索引)组成。仅由一组数据也可产生简单的Series对象

练习

import pandas as pd
import numpy as np
    

In [5]:

创建一个Series对象

s1 = pd.Series([4,6,-5,3])
print(s1)
0    4
1    6
2   -5
3    3
dtype: int64

In [8]:

获取Series的值

s1.values#获取值

Out[8]:

array([ 4,  6, -5,  3], dtype=int64)

In [9]:

获取Series索引

s1.index#获取索引

Out[9]:

RangeIndex(start=0, stop=4, step=1)

In [10]:

指定索引创建Series对象

s2 = pd.Series([4.0,6.5,212,2.6],index=['a','b','c','d'])#指定索引

In [11]:

print(s2)
a      4.0
b      6.5
c    212.0
d      2.6
dtype: float64

In [12]:

根据Series索引取值

s2["a"]#根据索引取值

Out[12]:

4.0

In [15]:

s2[['c','d']]#取多个索引值

Out[15]:

c    212.0
d      2.6
dtype: float64

In [16]:

判断索引是否在Series

'c' in s2#判断索引是否在Series

Out[16]:

True

In [17]:

'e' in s2

Out[17]:

False

In [18]:

series可以看成一个定长的有序字典

#series可以看成一个定长的有序字典
dic1 = {"apple":5,"pen":'3',"applenpen":10}
s3 = pd.Series(dic1)
print(s3)#构建后顺序是一定的,不能改变
apple         5
pen           3
applenpen    10
dtype: object

In [20]:

DataFrame 构造

#DataFrame 构造
data = {'year':[2015,2016,2017,2018],
       'income':[1000,2000,3000,4000],
       'pay':[100,200,300,400]}
df1 = pd.DataFrame(data)
df1

Out[20]:

year income pay
0 2015 1000 100
1 2016 2000 200
2 2017 3000 300
3 2018 4000 400

In [22]:

使用numpy构建dataframe

#使用numpy构建dataframe
df2 = pd.DataFrame(np.arange(12).reshape(3,4))
df2
'''
shape是查看数据有多少行多少列
reshape()是数组array中的方法,作用是将数据重新组织
'''

Out[22]:

0 1 2 3
0 0 1 2 3
1 4 5 6 7
2 8 9 10 11

In [24]:

指定索引和表头(第一列内容)构建dataframe

#指定索引和表头(第一列内容)
df3 = pd.DataFrame(np.arange(12).reshape(3,4),index=['a','b','c'],columns=["金","木","水","火"])
df3

Out[24]:

a 0 1 2 3
b 4 5 6 7
c 8 9 10 11

In [27]:

DataFrame的属性

#DataFrame的属性
df3.columns#列
#DataFrame的属性
df3.columns#列

Out[35]:

Index(['金', '木', '水', '火'], dtype='object')

In [28]:

Out[28]:

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

In [29]

df3.values#值,二位数组形式

Out[29]:

array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

In [30]:

df3.describe

Out[30]:

<bound method NDFrame.describe of    金  木   水   火
a  0  1   2   3
b  4  5   6   7
c  8  9  10  11>

In [31]:

转置

#转置
df3.T

Out[31]:

a b c
0 4 8
1 5 9
2 6 10
3 7 11

In [32]:

排序

#排序
df3.sort_index(axis=1)#axis=1是对列排序

Out[32]:

a 1 2 3 0
b 5 6 7 4
c 9 10 11 8

In [33]:

df3.sort_index(axis=0)#axis=0是对行排序

Out[33]:

a 0 1 2 3
b 4 5 6 7
c 8 9 10 11

In [34]:

#对某一列排序
df3.sort_index(by="金")
c:\users\wuzs\appdata\local\programs\python\python36-32\lib\site-packages\ipykernel_launcher.py:2: FutureWarning: by argument to sort_index is deprecated, please use .sort_values(by=...)
  

Out[34]:

a 0 1 2 3
b 4 5 6 7
c 8 9 10 11

猜你喜欢

转载自www.cnblogs.com/mrwuzs/p/11324510.html
今日推荐