Pandas

pandas的常用数据类型

1.Series 一维,带标签数组

2.DataFrame 二维,Series容器

pandas之Series创建

import pandas as pd
import numpy as np
import string

In [28]: s = pd.Series(np.arange(10),index=list(string.ascii_uppercase[:10])) # index行标签

In [29]: s
Out[29]:
A    0
B    1
C    2
D    3
E    4
F    5
G    6
H    7
I    8
J    9
dtype: int32

In [30]: type(s)
Out[30]: pandas.core.frame.Series

# 字典推导式创建一个Series,Series中的索引就是字典中的键
In [31]: a = {
   string.ascii_uppercase[i]:i for i in range(10)}

In [32]: a
Out[32]:
{
   'A': 0,
 'B': 1,
 'C': 2,
 'D': 3,
 'E': 4,
 'F': 5,
 'G': 6,
 'H': 7,
 'I': 8,
 'J': 9}

In [33]: pd.Series(a)
Out[33]:
A    0
B    1
C    2
D    3
E    4
F    5
G    6
H    7
I    8
J    9
dtype: int64

# 数据能够对应上,则是对应的值,不能对应上则为NaN,float类型。numpy中的NaN为float类型,pandas会自动根据数据类型更改Series的dtype类型
In [34]: pd.Series(a,index=list(string.ascii_uppercase[5:15]))
Out[34]:
F    5.0
G    6.0
H    7.0
I    8.0
J    9.0
K    NaN
L    NaN
M    NaN
N    NaN
O    NaN
dtype: float64

pandas之Series切片和索引

In [39]: a
Out[39]:
A    0
B    1
C    2
D    3
E    4
F    5
G    6
H    7
I    8
J    9
dtype: int64

In [40]: a[2:10:2]
Out[40]:
C    2
E    4
G    6
I    8
dtype: int64
    
In [42]: a[1]
Out[42]: 1
    
In [47]: a[[2,3,6]] # 取不连续的值
Out[47]:
C    2
D    3
G    6
dtype: int64

In [48]: a[a>4]
Out[48]:
F    5
G    6
H    7
I    8
J    9
dtype: int64

In [50]: a['F']
Out[50]: 5
    
In [51]: a[['A','F','g']]  #g元素没有,所以为NaN
Out[51]:
A    0.0
F    5.0
g    NaN
dtype: float64

切片:直接传入start,end,step即可

索引:一个的时候可以直接传入序号或index,多个的时候传入序号或者index的列表

pandas之Series的索引和值

对于一个陌生的series类型,我们如何知道他的索引和具体的值呢?

In [52]: a.index
Out[52]: Index(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'], dtype='object')

In [53]: a.values
Out[53]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=int64)

In [54]: type(a.index)
Out[54]: pandas.core.indexes.base.Index

In [55]: type(a.values)
Out[55]: numpy.ndarray

Series本质由两个数组组成,一个数组构成对象的键–>index索引,一个数组构成对象的值–>values值,即键值对(index,values)

pandas之读取外部数据

存在csv中,我们直接使用pd. read_csv即可,对于数据库比如mysql或者mongodb中数据,使用pd.read_sql(sql_sentence,connection)。但是它是一个DataFrame数据类型。

file_path = 'IMDB-Movie-Data.csv'
df = pd.read_csv(file_path)

pandas之DataFrame

DataFrame对象既有行索引,又有列索引

行索引,表明不同行,横向索引,叫index,0轴,axis=0
列索引,表名不同列,纵向索引,叫columns,1轴,axis=1

In [56]: t = pd.DataFrame(np.arange(12).reshape((3,4)))

In [57]: t
Out[57]:
   0  1   2   3
0  0  1   2   3
1  4  5   6   7
2  8  9  10  11

In [58]: t = pd.DataFrame(np.arange(12).reshape((3,4)),index=list(string.ascii_uppercase[:3]),columns=list
    ...: (string.ascii_uppercase[-4:]))  
In [59]: t
Out[59]:
   W  X   Y   Z
A  0  1   2   3
B  4  5   6   7
C  8  9  10  11

DataFrame的基础属性

在这里插入图片描述

In [60]: t.shape
Out[60]: (3, 4)

In [61]: t.shape[0]
Out[61]: 3

In [62]: t.dtypes
Out[62]:
W    int32
X    int32
Y    int32
Z    int32
dtype: object

In [64]: t.ndim
Out[64]: 2

In [65]: t.index
Out[65]: Index(['A', 'B', 'C'], dtype='object')

In [66]: t.columns
Out[66]: Index(['W', 'X', 'Y', 'Z'], dtype='object')

In [67]: t.values
Out[67]:
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

In [68]: t.head(3)
Out[68]:
   W  X   Y   Z
A  0  1   2   3
B  4  5   6   7
C  8  9  10  11

In [69]: t.head(2)
Out[69]:
   W  X  Y  Z
A  0  1  2  3
B  4  5  6  7

In [70]: t.tail(2)
Out[70]:
   W  X   Y   Z
B  4  5   6   7
C  8  9  10  11

In [71]: t.info()
<class 'pandas.core.frame.DataFrame'>
Index: 3 entries, A to C
Data columns (total 4 columns):
W    3 non-null int32
X    3 non-null int32
Y    3 non-null int32
Z    3 non-null int32
dtypes: int32(4)
memory usage: 72.0+ bytes

In [72]: t.describe()
Out[72]:
         W    X     Y     Z
count  3.0  3.0   3.0   3.0
mean   4.0  5.0   6.0   7.0
std    4.0  4.0   4.0   4.0
min    0.0  1.0   2.0   3.0
25%    2.0  3.0   4.0   5.0
50%    4.0  5.0   6.0   7.0
75%    6.0  7.0   8.0   9.0
max    8.0  9.0  10.0  11.0

排序

# 取Count_AnimalName这一列,降序来排序,默认升序。ascendind=False--->降序
df.sort_values(by="Count_AnimalName",ascending=False)  

# 使用次数前100的数据
df_sorted = df.sort_values(by="Count_AnimalName")
df_sorted[:100]

# 具体选择某一列
df[" Count_AnimalName "]

# 同时选择行和列
df[:100][" Count_AnimalName "]

loc和iloc

1.df.loc 通过标签索引行数据

2.df.iloc 通过位置获取行数据

In [73]: t
Out[73]:
   W  X   Y   Z
A  0  1   2   3
B  4  5   6   7
C  8  9  10  11

In [74]: t.loc['A','W']
Out[74]: 0

In [75]: t.loc['A',['W','Z']]
Out[75]:
W    0
Z    3
Name: A, dtype: int32

In [76]: type(t.loc['A',['W','Z']])
Out[76]: pandas.core.series.Series

In [77]: t.loc[['A','C'],['W','Z']]  # 选择间隔的多行多列
Out[77]:
   W   Z
A  0   3
C  8  11

In [78]: t.loc['A':,['W','Z']]
Out[78]:
   W   Z
A  0   3
B  4   7
C  8  11

In [79]: t.loc['A':'C',['W','Z']]   # C可以取到,闭合的
Out[79]:
   W   Z
A  0   3
B  4   7
C  8  11

In [81]: t.iloc[1:3,[2,3]]  # iloc行索引左开右闭,注意和loc的区别
Out[81]:
    Y   Z
B   6   7
C  10  11

In [82]: t.iloc[1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值