pandas

查看基本信息

import pandas as pd

ted = pd.read_csv('ted.csv')
ted.head()
ted.shape

#对每一列数据进行统计,包括计数,均值,std,各个分位数等。
data.describe() 

# 查看数据类型
ted.dtypes

# 查看每列缺失值数量
ted.isna().sum()

# 统计某一列x中各个值出现的次数
data['x'].value_counts()    

# 按某列排序
ted.sort_values('comments').tail()

# correct for this bias by calculating the number of comments pey view
ted['comments_per_view'] = ted.comments / ted.views

读取tsv文件

df = pd.read_csv(file, sep='\t')

# 如果已有表头
df = pd.read_csv(file, header=0)

删除某几列

pd.drop(axis=1, columns=['a', 'b'])

取行取列

1. loc函数

loc是用实际的索引

比如我们现在有这么一个DataFrame

# 取x行数据
df.loc['x']

# 取多行数据
df.loc['x': 'z']
 
# 索引某行某列
df.loc['x', ['b', 'c']]

# 索引某列
df.loc[:, 'b']

注意: DataFrame的索引[1:3]是包含3这个元素的的, 和其他地方有区别

2. iloc函数

iloc是用行号列号来索引(从0开始计数)

# 索引单行
df.iloc[0]

# 索引多行
df.iloc[0:]

# 索引列数据
df.iloc[:, [1]]  
3. ix函数

ix是结合了前两种的混合索引

# 通过行号索引
df.ix[0]

# 通过行标签索引
df.ix['x']

另外, 取列数据的话也可以不用上面函数直接这样取:

# 取a列
df['a']
df.a

第二种有个小问题, 就是列名不能有空格

持续更新…