福州搜索优化网站,预售网站开发,外贸网站定制,衡水营销网站建设Pandas 应用 Pandas 的主要数据结构是 Series #xff08;一维数据#xff09;与 DataFrame#xff08;二维数据#xff09;#xff0c;这两种数据结构足以处理金融、统计、社会科学、工程等领域里的大多数典型用例。 数据结构 Series 是一种类似于一维数组的对象#xf…Pandas 应用 Pandas 的主要数据结构是 Series 一维数据与 DataFrame二维数据这两种数据结构足以处理金融、统计、社会科学、工程等领域里的大多数典型用例。 数据结构 Series 是一种类似于一维数组的对象它由一组数据各种Numpy数据类型以及一组与之相关的数据标签即索引组成。 DataFrame 是一个表格型的数据结构它含有一组有序的列每列可以是不同的值类型数值、字符串、布尔型值。DataFrame 既有行索引也有列索引它可以被看做由 Series 组成的字典共同用一个索引。
1、Pandas 数据结构 - Series
Series 带标签的一维数组 pandas.Series( data, index, dtype, name, copy)
参数说明
data一组数据(ndarray 类型)。index数据索引标签如果不指定默认从 0 开始。dtype数据类型默认会自己判断。name设置名称。copy拷贝数据默认为 False。
如果没有指定索引索引值就从 0 开始
t pd.Series([4,5,6])
print(t)
print(type(t)) # class pandas.core.series.Series
print(t[1]) # 50 4
1 5
2 6
dtype: int64
class pandas.core.series.Series
5指定索引值修改数据类型
t2 pd.Series([2,4,6,8], indexlist(abcd))
print(t2)
print(t2[c]) # 6
print(t2.astype(float))
print(t2[t25])a 2
b 4
c 6
d 8
dtype: int64
6
a 2.0
b 4.0
c 6.0
d 8.0
dtype: float64
c 6
d 8
dtype: int64使用 key/value 对象类似字典来创建 Series
temp_dict {name: wang1, age: 18, tel: 10010}t3 pd.Series(temp_dict)
print(t3)
print(t3[age]) # 18
print(t3[1]) # 18
print(t3[:2])
print(t3[[1,2]])
print(t3[[name,tel]])name wang1
age 18
tel 10010
dtype: object
18
18
name wang1
age 18
dtype: object
age 18
tel 10010
dtype: object
name wang1
tel 10010
dtype: object获取 Series 的值、索引
print(t3.index) # Index([name, age, tel], dtypeobject)
print(type(t3.index)) # class pandas.core.indexes.base.Indexprint(t3.values) # [wang1 18 10010]
print(type(t3.values)) # class numpy.ndarray2、Pandas 数据结构 - DataFrame
DataFrame 是一个表格型的数据结构它含有一组有序的列每列可以是不同的值类型数值、字符串、布尔型值。DataFrame 既有行索引也有列索引它可以被看做由 Series 组成的字典共同用一个索引 DataFrame 构造方法如下 pandas.DataFrame( data, index, columns, dtype, copy) DataFrame 二维Series 容器 参数说明
data一组数据(ndarray、series, map, lists, dict 等类型)。index索引值或者可以称为行标签。columns列标签默认为 RangeIndex (0, 1, 2, …, n) 。dtype数据类型。copy拷贝数据默认为 False。
import pandas as pd
import numpy as np
t pd.DataFrame(np.arange(12).reshape(3,4))
print(t)
0 1 2 3
0 0 1 2 3
1 4 5 6 7
2 8 9 10 11DataFrame对象既有行索引又有列索引 行索引表明不同行横向索引叫index0轴axis0 列索引表明不同列纵向索引叫columns1轴axis1
2.1、index、columns 使用
t1 pd.DataFrame(np.arange(12).reshape(3,4), indexlist(abc), columnslist(wxyz))
print(t1)
w x y z
a 0 1 2 3
b 4 5 6 7
c 8 9 10 112.2、使用列表创建DataFrame缺失的值用 NaN 代替
data [[Google,10],[Runoob,12],[Wiki,13]]
df pd.DataFrame(data,columns[Site,Age])
print(df)
Site Age
0 Google 10
1 Runoob 12
2 Wiki 132.3、使用字典创建DataFrame缺失的值用 NaN 代替
data {Site:[Google, Runoob, Wiki], Age:[10, 12, 13]}
df pd.DataFrame(data)
print (df)
Site Age
0 Google 10
1 Runoob 12
2 Wiki 13data [{a: 1, b: 2},{a: 5, b: 10, c: 20}]
df pd.DataFrame(data)
print (df)
a b c
0 1 2 NaN
1 5 10 20.02.4、DataFrame基础属性
DataFrame.shape # 行数 列数
DataFrame.dtypes # 列数据类型
DataFrame.ndim # 数据维度
DataFrame.index # 行索引
DataFrame.columns # 列索引
DataFrame.values # 对象值DataFrame.head(3) # 显示头部几行默认5行
DataFrame.tail(3) # 显示末尾几行默认5行
DataFrame.info() # 相关信息概览行数列数列索引列非空值个数列类型内存占用
DataFrame.describe() # 快速综合统计结果计数均值标准差最大值四分位数最小值data [{a: 1, b: 2},{a: 5, b: 10, c: 20}]
df pd.DataFrame(data)
print(df)
a b c
0 1 2 NaN
1 5 10 20.0print(df.index) # RangeIndex(start0, stop2, step1)
print(df.columns) # Index([a, b, c], dtypeobject)
print(df.values) # [[ 1. 2. nan] [ 5. 10. 20.]]
print(df.shape) # (2, 3)
print(df.ndim) # 数据维度 2
print(df.dtypes) # 列数据类型a int64
b int64
c float64
dtype: objectprint(**80)
print(df.info())class pandas.core.frame.DataFrame
RangeIndex: 2 entries, 0 to 1
Data columns (total 3 columns):# Column Non-Null Count Dtype
--- ------ -------------- ----- 0 a 2 non-null int64 1 b 2 non-null int64 2 c 1 non-null float64
dtypes: float64(1), int64(2)
memory usage: 176.0 bytes
Noneprint(df.describe())
a b c
count 2.000000 2.000000 1.0
mean 3.000000 6.000000 20.0
std 2.828427 5.656854 NaN
min 1.000000 2.000000 20.0
25% 2.000000 4.000000 20.0
50% 3.000000 6.000000 20.0
75% 4.000000 8.000000 20.0
max 5.000000 10.000000 20.02.5、DataFrame 排序
data [{a: 1, b: 2},{a: 5, b: 10, c: 20}]
df pd.DataFrame(data)
print(df)
a b c
0 1 2 NaN
1 5 10 20.0# ascendingTrue 升序
# ascendingFalse 降序
df df.sort_values(c, ascendingFalse)
print(df)
a b c
1 5 10 20.0
0 1 2 NaNhttps://www.runoob.com/pandas/pandas-series.html https://www.bilibili.com/video/BV1hx411d7jb?p23 https://www.bilibili.com/video/BV1hx411d7jb?p24 https://www.bilibili.com/video/BV1hx411d7jb?p25 https://www.bilibili.com/video/BV1hx411d7jb?p26