3.5.1 pandas基础

1.导入三方库

import numpy as np
import pandas as pd

查看库的版本

pd.__version__
'1.0.5'

2.数据文件的导入和导出

1)xlsx文件

shuju_xlsx = pd.read_excel('table.xlsx')
shuju_xlsx2 = pd.read_excel('可视化图表案例数据.xlsx')
shuju_xlsx4 = pd.read_excel('可视化图表案例数据.xlsx',sheet_name='条形图')

2)csv文件

shuju_csv = pd.read_csv('table.csv')

3)txt文件

shuju_txt = pd.read_table('table.txt')
shuju_txt
.dataframe tbody tr th {
  vertical-align: top;
}

.dataframe thead th {
  text-align: right;
}

</style>

 col1col2col3col4
02a1.4apple
13b3.4banana
26c2.5orange
35d3.2lemon

</div>

shuju_txt2 = pd.read_table('citydata.txt',sep='\s+')

3.基本数据结构

1)Series 常用的属性有:值(values)、索引(index)、名字(name)、类型(dtype)

se = pd.Series(np.random.randint(60,100,(8)),index=['s','e','q','b','a','s','h','t'],name='模拟学生成绩',dtype='int32')

se
s    69
e    96
q    74
b    88
a    66
s    73
h    97
t    64
Name: 模拟学生成绩, dtype: int32
se['e']
96
se['s']
s    69
s    73
Name: 模拟学生成绩, dtype: int32

2)DataFrame

a)创建DataFrame

DF = pd.DataFrame({'字母':list('abcdefgh'),'编号':range(5,13),'分值':[1.3,1,3.6,4.6,18,9.1,11,11.111]},
                  index=list('一二三四五六七八'))

DF
.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

 字母编号分值
a51.300
b61.000
c73.600
d84.600
e918.000
f109.100
g1111.000
h1211.111

</div>

b)从DataFrame中取出一列

DF[['编号','分值']]
.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

 编号分值
51.300
61.000
73.600
84.600
918.000
109.100
1111.000
1211.111

</div>

c)修改行或列名

修改列名

DF.rename(columns={'asd':'字母','shuzi':'编号','带小数点的数据':'分值'})
.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

 字母编号分值
a51.300
b61.000
c73.600
d84.600
e918.000
f109.100
g1111.000
h1211.111

</div>

#1.与直接数据提供人或者数据导出者对接
#2.1)让对方自行内部沟通,等待结果;2)尝试获取或切入对方的数据录入环节

修改行名

DF.rename(index={'一':'numberone'},inplace=True)

DF
.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

 字母编号分值
numberonea51.300
b61.000
c73.600
d84.600
e918.000
f109.100
g1111.000
h1211.111

</div>

d)调用属性和方法

DF.values
array([['a', 5, 1.3],
       ['b', 6, 1.0],
       ['c', 7, 3.6],
       ['d', 8, 4.6],
       ['e', 9, 18.0],
       ['f', 10, 9.1],
       ['g', 11, 11.0],
       ['h', 12, 11.111]], dtype=object)
DF.index
Index(['numberone', '二', '三', '四', '五', '六', '七', '八'], dtype='object')
DF.columns
Index(['字母', '编号', '分值'], dtype='object')

e)列的删除和添加

列的删除

DF.drop(index='五',columns='字母') #使用参数inplace=True才会在DataFrame中生效
.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

 编号分值
numberone51.300
61.000
73.600
84.600
109.100
1111.000
1211.111

</div>

del DF['字母'] #执行后改动就会生效
DF
.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

 编号分值
numberone51.300
61.000
73.600
84.600
918.000
109.100
1111.000
1211.111

</div>

列的添加

DF
.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

 编号分值
numberone51.300
61.000
73.600
84.600
918.000
109.100
1111.000
1211.111

</div>

DF['添加的列'] = DF['编号'] + DF['分值']
DF
.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

 编号分值添加的列
numberone51.3006.300
61.0007.000
73.60010.600
84.60012.600
918.00027.000
109.10019.100
1111.00022.000
1211.11123.111

</div>

f)根据类型选择列

DF.select_dtypes(include=['number'])
.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

 编号分值添加的列
numberone51.3006.300
61.0007.000
73.60010.600
84.60012.600
918.00027.000
109.10019.100
1111.00022.000
1211.11123.111

</div>

DF.select_dtypes(include=['object'])
.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

 
numberone

</div>

4.常用基本函数

导入数据文件

csv_shuju = pd.read_csv('table.csv')

a)head和tail

csv_shuju.head()
.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

 SchoolClassIDGenderAddressHeightWeightMathPhysics
0S_1C_11101Mstreet_11736334.0A+
1S_1C_11102Fstreet_21927332.5B+
2S_1C_11103Mstreet_21868287.2B+
3S_1C_11104Fstreet_21678180.4B-
4S_1C_11105Fstreet_41596484.8B+

</div>

csv_shuju.rename(columns={'School':'学校','Class':'班级','ID':'学生ID','Gender':'性别',
                          'Address':'地址','Height':'身高','Weight':'体重','Math':'数学成绩','Physics':'物理成绩'},inplace=True)
csv_shuju.head(1) #默认显示5行
.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

 学校班级学生ID性别地址身高体重数学成绩物理成绩
0S_1C_11101Mstreet_11736334.0A+

</div>

csv_shuju.tail(2) #默认显示5行
.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

 学校班级学生ID性别地址身高体重数学成绩物理成绩
33S_2C_42404Fstreet_21608467.7B
34S_2C_42405Fstreet_61935447.6B

</div>

b)unique和nunique

csv_shuju['性别'].nunique() #nunique显示该字段里有多少个唯一值
2
csv_shuju['性别'].unique() #unique显示该字段里所有的唯一值
array(['M', 'F'], dtype=object)

c)count和value_counts

csv_shuju['地址'].count() #返回非缺失值元素的个数
35
csv_shuju['物理成绩'].value_counts() #返回每个元素的频数(有多少个)
#不太适合交易额、销量、分值
B+    9
B     8
B-    6
A     4
A-    3
A+    3
C     2
Name: 物理成绩, dtype: int64

d)info和describe

csv_shuju.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 35 entries, 0 to 34
Data columns (total 9 columns):
 #   Column  Non-Null Count  Dtype  
---  ------  --------------  -----  
 0   学校      35 non-null     object 
 1   班级      35 non-null     object 
 2   学生ID    35 non-null     int64  
 3   性别      35 non-null     object 
 4   地址      35 non-null     object 
 5   身高      35 non-null     int64  
 6   体重      35 non-null     int64  
 7   数学成绩    35 non-null     float64
 8   物理成绩    35 non-null     object 
dtypes: float64(1), int64(3), object(5)
memory usage: 2.6+ KB
csv_shuju.describe() #统计数值型数据的各个统计量
.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

 学生ID身高体重数学成绩
count35.0000035.00000035.00000035.000000
mean1803.00000174.14285774.65714361.351429
std536.8774113.54109812.89537719.915164
min1101.00000155.00000053.00000031.500000
25%1204.50000161.00000063.00000047.400000
50%2103.00000173.00000074.00000061.700000
75%2301.50000187.50000082.00000077.100000
max2405.00000195.000000100.00000097.000000

</div>

csv_shuju.describe(percentiles=[.05,.25,.75,.95])
.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

 学生ID身高体重数学成绩
count35.0000035.00000035.00000035.000000
mean1803.00000174.14285774.65714361.351429
std536.8774113.54109812.89537719.915164
min1101.00000155.00000053.00000031.500000
5%1102.70000157.00000056.10000032.640000
25%1204.50000161.00000063.00000047.400000
50%2103.00000173.00000074.00000061.700000
75%2301.50000187.50000082.00000077.100000
95%2403.30000193.30000097.60000090.040000
max2405.00000195.000000100.00000097.000000

</div>

csv_shuju['物理成绩'].describe()
count     35
unique     7
top       B+
freq       9
Name: 物理成绩, dtype: object

e)idxmax和nlargest

idxmax

csv_shuju['数学成绩'].idxmax() #返回最大值所在行的索引
5
csv_shuju[5:9]
.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

 学校班级学生ID性别地址身高体重数学成绩物理成绩
5S_1C_21201Mstreet_51886897.0A-
6S_1C_21202Fstreet_41769463.5B-
7S_1C_21203Mstreet_61605358.8A+
8S_1C_21204Fstreet_51626333.8B

</div>

csv_shuju['数学成绩'].idxmin() #返回最小值所在行的索引
10
csv_shuju[10:20]
.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

 学校班级学生ID性别地址身高体重数学成绩物理成绩
10S_1C_31301Mstreet_41616831.5B+
11S_1C_31302Fstreet_11755787.7A-
12S_1C_31303Mstreet_71888249.7B
13S_1C_31304Mstreet_21957085.2A
14S_1C_31305Fstreet_51876961.7B-
15S_2C_12101Mstreet_71748483.3C
16S_2C_12102Fstreet_61616150.6B+
17S_2C_12103Mstreet_41576152.5B-
18S_2C_12104Fstreet_51599772.2B+
19S_2C_12105Mstreet_41708134.2A

</div>

nlargest

csv_shuju['数学成绩'].nlargest() #返回前几个大的元素值及其索引
5     97.0
28    95.5
11    87.7
2     87.2
24    85.4
Name: 数学成绩, dtype: float64
csv_shuju['数学成绩'].nsmallest() #返回前几个小的元素值及其索引
10    31.5
1     32.5
26    32.7
8     33.8
0     34.0
Name: 数学成绩, dtype: float64

e)clip和replace

clip:对超过或低于指定值的数据进行截取

csv_shuju['数学成绩'].clip(31,50).head()
0    34.0
1    32.5
2    50.0
3    50.0
4    50.0
Name: 数学成绩, dtype: float64

replace:对指定值进行替换

csv_shuju['地址'].replace(['street_1','street_2','street_4'],['one','two','fouth'],inplace=True)
csv_shuju.head(10)
.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

</style>

 学校班级学生ID性别地址身高体重数学成绩物理成绩
0S_1C_11101Mone1736334.0A+
1S_1C_11102Ftwo1927332.5B+
2S_1C_11103Mtwo1868287.2B+
3S_1C_11104Ftwo1678180.4B-
4S_1C_11105Ffouth1596484.8B+
5S_1C_21201Mstreet_51886897.0A-
6S_1C_21202Ffouth1769463.5B-
7S_1C_21203Mstreet_61605358.8A+
8S_1C_21204Fstreet_51626333.8B
9S_1C_21205Fstreet_61676368.4B-

</div>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小石小石摩西摩西

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值