需要用到的基础知识
pandas基础知识
参考1,2章
https://github.com/datawhalechina/joyful-pandas

1.导入数据
tsv 制表符作为分隔符的字段符
csv 逗号作为分隔符的字段符
详情见利用python进行数据分析第6章
https://github.com/Knowledge-Discovery-in-Databases/team-learning/blob/master/%E7%AC%AC06%E7%AB%A0%20%E6%95%B0%E6%8D%AE%E5%8A%A0%E8%BD%BD%E3%80%81%E5%AD%98%E5%82%A8%E4%B8%8E%E6%96%87%E4%BB%B6%E6%A0%BC%E5%BC%8F.md

```
#导入包
import numpy as np
import pandas as pd
import os
```

```
#查看当前工作目录,修改当前目录,命令行查看当前工作目录
print(os.getcwd())
os.chdir('/Users/mofashipython')
!pwd

/Users/mofashipython/prog/p
/Users/mofashipython
```

```
#当前工作目录导入文件
df = pd.read_csv('train.csv')
df.head() #查看开头5行,可设定参数

#绝对目录导入文件
df = pd.read_csv('/Users/mofashipython/train.csv')
df.tail() #查看末尾5行,可设定参数

```

```
#查看信息(数据结构)
df.info()
```
```
#分块
chunker = pd.read_csv('train.csv', chunksize=1000)

```

```
#修改行标签和列标签
df = pd.read_csv('file', names=name1,index_col='name2',header=0)

```

```
#查找空值
df.isnull().head()
```

```
#查看列名和行名
df.columns
df.index
```

```
#查看常用的统计数值
df.describe()
'''
count : 样本数据大小
mean : 样本数据的平均值
std : 样本数据的标准差
min : 样本数据的最小值
25% : 样本数据25%的时候的值
50% : 样本数据50%的时候的值
75% : 样本数据75%的时候的值
max : 样本数据的最大值
'''
```

```
df['列名'].describe()
```

```
#保存文件至当前目录
df.to_csv('train_chinese.csv')

```

2.pandas的基本使用方法
pandas中有两个数据类型DataFrame和Series
series,一维数据结构,由index和value组成。
dataframe,二维结构,拥有index和value和column。
dataframe由多个series组成,可以从series创建

```
#创建Series
v=[1,2,3,4,5]
i=[2,3,4,5,6]
s = pd.Series(v,index = i)
s

2 1
3 2
4 3
5 4
6 5
dtype: int64

```

```
#创建DataFrame
i =[1,2,3]
c =["one", "two", "three"]
v = np.random.rand(9).reshape(3,3)
d = pd.DataFrame(v, index = i, columns = c)
d

one two three
1 0.491216 0.826787 0.002878
2 0.751016 0.849535 0.738048
3 0.066599 0.268772 0.210717

```

```
#对应的行和列的值会相加,没有对应的会变成空值NaN
frame1_a = pd.DataFrame(np.arange(9.).reshape(3, 3),
columns=['a', 'b', 'c'],
index=['one', 'two', 'three'])
frame1_b = pd.DataFrame(np.arange(12.).reshape(4, 3),
columns=['a', 'e', 'c'],
index=['first', 'one', 'two', 'second'])
frame1_a + frame1_b

a b c e
first NaN NaN NaN NaN
one 3.0 NaN 7.0 NaN
second NaN NaN NaN NaN
three NaN NaN NaN NaN
two 9.0 NaN 13.0 NaN
```

3.常用的3种索引方法
df.iloc 位置(数字)索引
df.loc 名称(标签)索引
[] 切片索引
df.loc[行索引,列索引]
逗号隔开维度,loc 左闭右闭区间,可使用布尔型(true,false)

```
#全部行,Cabin列,输出前5个
df.loc[:,'Cabin'].head()
```

```
#重置索引,使用默认的行索引
#不保留原索引,需要参数drop=True
df= df.reset_index(drop=True)
```

```
#100,105,108行,Pclass,Name,Sex列(标签值)
df.loc[[100,105,108],['Pclass','Name','Sex']]

```

```
#100,105,108行,2,3,4列(索引值)
df.iloc[[100,105,108],[2,3,4]]

```

```
#条件查询(布尔型查询)
df[df["Age"]<10].head(3)
midage = df[(df["Age"]>10)& (df["Age"]<50)]
```

4.排序
sort_values 数值进行排序
sort_index 标签进行排序
```
i =[1,2,3]
c =["one", "two", "three"]

v = np.arange(9).reshape(3,3)
d = pd.DataFrame(v, index = i, columns = c)
d

one two three
1 0 1 2
2 3 4 5
3 6 7 8

#two列降序(从大到小)
d.sort_values(by='two',ascending = False)
one two three
3 6 7 8
2 3 4 5
1 0 1 2

#axis =1 维度是列,按照列名排序
d.sort_index(axis = 1,ascending = False)
two three one
1 1 2 0
2 4 5 3
3 7 8 6

#one列和three列升序(从小到大)
#首先排序one列,如果one列存在相同元素,排three列
#如果没有相同元素,不排three列
d.sort_values(by=['one','three'])
one two three
1 0 1 2
2 3 4 5
3 6 7 8

```

泰坦尼克项目内容
```
#导入包
import numpy as np
import pandas as pd

#绝对目录导入文件
df = pd.read_csv('/Users/mofashipython/train.csv')

#把列标签改成中文
df = pd.read_csv('train.csv', names=['乘客ID','是否幸存','仓位等级','姓名','性别','年龄','兄弟姐妹个数','父母子女个数','船票信息','票价','客舱','登船港口'],index_col='乘客ID',header=0)
#查看信息(数据结构)
df.info()

```
<class 'pandas.core.frame.DataFrame'>
Int64Index: 891 entries, 1 to 891
Data columns (total 11 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 是否幸存 891 non-null int64
1 仓位等级 891 non-null int64
2 姓名 891 non-null object
3 性别 891 non-null object
4 年龄 714 non-null float64
5 兄弟姐妹个数 891 non-null int64
6 父母子女个数 891 non-null int64
7 船票信息 891 non-null object
8 票价 891 non-null float64
9 客舱 204 non-null object
10 登船港口 889 non-null object
dtypes: float64(2), int64(4), object(5)
memory usage: 83.5+ KB

```
#查找空值
df.isnull().head()
```

```
#根据票价,年龄,降序排序
text.sort_values(by=['票价', '年龄'], ascending=False).head(10)
```
10个票价最高(相对富有)的人中,8个人存活。

```
#票价列的常用统计值
text['票价'].describe()
```
count 891.000000
mean 32.204208
std 49.693429
min 0.000000
25% 7.910400
50% 14.454200
75% 31.000000
max 512.329200
Name: 票价, dtype: float64

平均值约为:32.20,
标准差约为49.69,票价波动大

中位数14.45 远低于平均数
75% 31.00 接近平均数

```
```

最新文章

  1. nginx源码分析之模块初始化
  2. 更换app开发者账号
  3. x01.os.14: 时间都去哪儿了
  4. jQuery提供的小方法
  5. LINUX下为LVM磁盘增加硬盘空间
  6. webform--常用的控件
  7. 【OCR技术系列之二】文字定位与切割
  8. C# .net中json字符串和对象之间的转化方法
  9. GIT命令一页纸
  10. VisualSVN设置提交时必须输入日志信息
  11. linux git 保存账号密码
  12. sqlserver2008数据库自动备份的sql脚本及使用bat命令执行脚本
  13. ceph 安装ceph问题汇总
  14. jquery所有版本下载外链地址
  15. go,gcvis,golang, privoxy,and git proxy
  16. 团队作业4——WBS练习
  17. 从UE(用户体验)到道家誓学再到李小龙
  18. 雷林鹏分享:Ruby 变量
  19. &#39;org.springframework.beans.factory.xml.XmlBeanFactory&#39; is deprecated
  20. mv命令详解

热门文章

  1. DJANGO-天天生鲜项目从0到1-002-用户模块-注册
  2. scrapy分布式浅谈+京东示例
  3. 事件流和初识Jquery
  4. spring boot 项目连接数据库查询数据过程
  5. LQB201804第几个幸运数
  6. PDOStatement::execute
  7. NOI Online 游戏 树形dp 广义容斥/二项式反演
  8. 关于c/c++中的二维数组与指针
  9. 在Swoole上加速Laravel应用
  10. (转)海思平台HI35XX系列内存设置