pandas学习01

pandas的两种重要的数据类型DataFrame和Series
import numpy as np
import pandas as pd
import os
os.getcwd()#查找当前工作目录
'C:\\Users\\dell'
#DataFrame相当于有表格,有行表头和列表头。在数据分析中很少直接定义一般采用外部导入文件
df1 = pd.DataFrame(np.arange(10).reshape(2,5))
df1
01234
001234
156789
df2=pd.read_csv(r'C:\Users\dell\data.csv')
df2.head()
#注意当pandas导入外部文件时候将会自动转换为DataFrame对象
accountnamestreetcitystatepostal-codeJanFebMar
0211829Kerluke, Koepp and Hilpert34456 Sean HighwayNew JaycobTexas28752100006200035000
1320563Walter-Trantow1311 Alvis TunnelPort KhadijahNorthCarolina38365950004500035000
2648336Bashirian, Kunde and Price62184 Schamberger Underpass Apt. 231New LilianlandIowa765179100012000035000
3109996D'Amore, Gleichner and Bode155 Fadel Crescent Apt. 144HyattburghMaine460214500012000010000
4121213Bauch-Goldner7274 Marissa CommonShanahanchesterCalifornia4968116200012000035000
#查看行名index属性
df2.index
#查看行数
df2.index.size
#查看列名
df2.columns
df2.columns.size
9
df2.shape#查看行列数目即形状 
(15, 9)
#计算行列书的另一个方法
print("行数为",df2.shape[0])
print("列数为",df2.shape[1])
行数为 15
列数为 9
#访问元素的写法——按照列名读取
df2["name"].head()#第一种方法列名出现在下标
df2[name][2]#注意当列名和行号一起使用的时候数据框的第0轴是列
0     Kerluke, Koepp and Hilpert
1                 Walter-Trantow
2     Bashirian, Kunde and Price
3    D'Amore, Gleichner and Bode
4                  Bauch-Goldner
Name: name, dtype: object
df2.name.head()#列名当做数据框的一个属性来使用
df2.name[2]
0     Kerluke, Koepp and Hilpert
1                 Walter-Trantow
2     Bashirian, Kunde and Price
3    D'Amore, Gleichner and Bode
4                  Bauch-Goldner
Name: name, dtype: object
df2["city"][[2,5]]#fancy indexing
2    New Lilianland
5      Jeremieburgh
Name: city, dtype: object
#访问元素的写法——按照索引的方式 注意都用中括符
#loc标识显示索引 iloc标识隐式索引
df2.loc[1,"street"]
'1311 Alvis Tunnel'
df2.iloc[1,2]#先列在行
'1311 Alvis Tunnel'
#删除列的方法——del语句
del df2["street"]
df2.head()
accountnamecitystatepostal-codeJanFebMar
0211829Kerluke, Koepp and HilpertNew JaycobTexas28752100006200035000
1320563Walter-TrantowPort KhadijahNorthCarolina38365950004500035000
2648336Bashirian, Kunde and PriceNew LilianlandIowa765179100012000035000
3109996D'Amore, Gleichner and BodeHyattburghMaine460214500012000010000
4121213Bauch-GoldnerShanahanchesterCalifornia4968116200012000035000
#drop()删除或过滤不改变数据框对象本身
df3=df2[["account","name","city"]]
df3.drop(["name","city"],axis=1,inplace=True)
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py:3997: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  errors=errors,
#进行数据过滤
df2[df2.Feb>46000].head()
df2[df2.Jan>46000][["city","name"]].head()
df2[df2.city=='New Jaycob'].count() #频数统计
account        1
name           1
city           1
state          1
postal-code    1
Jan            1
Feb            1
Mar            1
dtype: int64
dff=df2[["name","Feb"]]
dff.sort_values(by="Feb",axis=0,ascending=True).head()#列按照值排序
nameFeb
11Hahn-Moore10000
1Walter-Trantow45000
0Kerluke, Koepp and Hilpert62000
7Kovacek-Johnston95000
8Champlin-Morar95000


算数运算

#规则:数据框之间计算会补齐行列索引(新增加的行列索引对应值为NaN)得到相同结构之后进行计算
df4=pd.DataFrame(np.arange(6).reshape(2,3))
df4
012
0012
1345
df5=pd.DataFrame(np.arange(10).reshape(2,5))
df5
01234
001234
156789
df4+df5
01234
0024NaNNaN
181012NaNNaN
#但是在数据分析中一般不用直接的运算符二十采用相应的函数,因为调用函数的灵活性更高而且可以设置更多的参数、计算方向等
df6=df4.add(df5,fill_value=10)
#数据框和series按行计算:先将行(第1轴)广播,把行改为等长,行内不作循环补齐,只是一行行计算而不跨行广播
s1=pd.Series(np.arange(3))
df6-s1
#等价于df5.sub(s1,axis=1)
01234
00.01.02.0NaNNaN
18.09.010.0NaNNaN
df4.rolling(2).sum#按列依次计算相邻两个元素的和,即为本元素和上一个元素的和
<bound method Rolling.sum of Rolling [window=2,center=False,axis=0]>
df4.cov()#协方差矩阵
012
04.54.54.5
14.54.54.5
24.54.54.5
df4.corr()#相关稀疏 矩阵
012
01.01.01.0
11.01.01.0
21.01.01.0
df7=df4.T
df7#矩阵转置
01
003
114
225

缺失值处理

#判断一个数据框是否为空的方法——属性empty
df3.empty
False
#注意在python的基础语法里None和NaN处理方法 是不一样的
#在python基础语法里None不能参加计算但是NaN可以参加计算
#在pandas中二者都可以参加计算
np.nan+1
np.nan-np.nan
nan
A=pd.DataFrame(np.array([10,10,20,20]).reshape(2,2),columns=list("ab"),index=list("sw"))
A
A.stack()
s  a    10
   b    10
w  a    20
   b    20
dtype: int32
A.mean()
a    15.0
b    15.0
dtype: float64
#数据框处理缺失值的四个重要函数 isnull notnull dropna fillna
A.notnull()
ab
sTrueTrue
wTrueTrue
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值