Home  >  Article  >  Backend Development  >  Methods of DataFrame type data operation functions in python pandas

Methods of DataFrame type data operation functions in python pandas

不言
不言Original
2018-05-05 15:41:545426browse

这篇文章主要介绍了关于python pandas中DataFrame类型数据操作函数的方法,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

python数据分析工具pandas中DataFrame和Series作为主要的数据结构.

本文主要是介绍如何对DataFrame数据进行操作并结合一个实例测试操作函数。

1)查看DataFrame数据及属性

df_obj = DataFrame() #创建DataFrame对象
df_obj.dtypes #查看各行的数据格式
df_obj['列名'].astype(int)#转换某列的数据类型
df_obj.head() #查看前几行的数据,默认前5行
df_obj.tail() #查看后几行的数据,默认后5行
df_obj.index #查看索引
df_obj.columns #查看列名
df_obj.values #查看数据值
df_obj.describe() #描述性统计
df_obj.T #转置
df_obj.sort_values(by=['',''])#同上

2)使用DataFrame选择数据:

df_obj.ix[1:3] #获取1-3行的数据,该操作叫切片操作,获取行数据
df_obj.ix[columns_index] #获取列的数据
df_obj.ix[1:3,[1,3]]#获取1列3列的1~3行数据
df_obj[columns].drop_duplicates() #剔除重复行数据

3)使用DataFrame重置数据:

df_obj.ix[1:3,[1,3]]=1#所选位置数据替换为1

4)使用DataFrame筛选数据(类似SQL中的WHERE):

alist = ['023-18996609823']
df_obj['用户号码'].isin(alist) #将要过滤的数据放入字典中,使用isin对数据进行筛选,返回行索引以及每行筛选的结果,若匹配则返回ture
df_obj[df_obj['用户号码'].isin(alist)] #获取匹配结果为ture的行

5)使用DataFrame模糊筛选数据(类似SQL中的LIKE):

df_obj[df_obj['套餐'].str.contains(r'.*?语音CDMA.*')] #使用正则表达式进行模糊匹配,*匹配0或无限次,?匹配0或1次

6)使用DataFrame进行数据转换(后期补充说明)

df_obj['支局_维护线'] = df_obj['支局_维护线'].str.replace('巫溪分公司(.{2,})支局','\\1')#可以使用正则表达式
可以设置take_last=ture 保留最后一个,或保留开始一个.补充说明:注意take_last=ture已过时,请使用keep='last'

7)使用pandas中读取数据:

read_csv('D:\LQJ.csv',sep=';',nrows=2) #首先输入csv文本地址,然后分割符选择等等
df.to_excel('foo.xlsx',sheet_name='Sheet1');pd.read_excel('foo.xlsx', 'Sheet1', index_col=None, na_values=['NA'])#写入读取excel数据,pd.read_excel读取的数据是以DataFrame形式存储
df.to_hdf('foo.h5','df');pd.read_hdf('foo.h5','df')#写入读取HDF5数据

8)使用pandas聚合数据(类似SQL中的GROUP BY 或HAVING):

data_obj['用户标识'].groupby(data_obj['支局_维护线'])
data_obj.groupby('支局_维护线')['用户标识'] #上面的简单写法
adsl_obj.groupby('支局_维护线')['用户标识'].agg([('ADSL','count')])#按支局进行汇总对用户标识进行计数,并将计数列的列名命名为ADSL

9)使用pandas合并数据集(类似SQL中的JOIN):

merge(mxj_obj2, mxj_obj1 ,on='用户标识',how='inner')# mxj_obj1和mxj_obj2将用户标识当成重叠列的键合并两个数据集,inner表示取两个数据集的交集.

10)清理数据

df[df.isnull()]
df[df.notnull()]
df.dropna()#将所有含有nan项的row删除
df.dropna(axis=1,thresh=3) #将在列的方向上三个为NaN的项删除
df.dropna(how='ALL')#将全部项都是nan的row删除填充值
df.fillna(0)
df.fillna({1:0,2:0.5}) #对第一列nan值赋0,第二列赋值0.5
df.fillna(method='ffill') #在列方向上以前一个值作为值赋给NaN

实例

1. 读取excel数据

代码如下

import pandas as pd# 读取高炉数据,注意文件名不能为中文
data=pd.read_excel('gaolushuju_201501-03.xlsx', '201501', index_col=None, na_values=['NA'])
print data

测试结果如下

   燃料比 顶温西南 顶温西北 顶温东南 顶温东北
0  531.46  185  176  176  174
1  510.35  184  173  184  188
2  533.49  180  165  182  177
3  511.51  190  172  179  188
4  531.02  180  167  173  180
5  511.24  174  164  178  176
6  532.62  173  170  168  179
7  583.00  182  175  176  173
8  530.70  158  149  159  156
9  530.32  168  156  169  171
10 528.62  164  150  171  169

2. 切片处理,选取行或列,修改数据

代码如下:

data_1row=data.ix[1]
data_5row_2col=data.ix[0:5,[u'燃料比',u'顶温西南']
print data_1row,data_5row_2col
data_5row_2col.ix[0:1,0:2]=3

测试结果如下:

燃料比   510.35
顶温西南  184.00
顶温西北  173.00
顶温东南  184.00
顶温东北  188.00
Name: 1, dtype: float64  
  燃料比 顶温西南
0 531.46  185
1 510.35  184
2 533.49  180
3 511.51  190
4 531.02  180
5 511.24  174
   燃料比 顶温西南
0  3.00   3
1  3.00   3
2 533.49  180
3 511.51  190
4 531.02  180
5 511.24  174

格式说明,data_5row_2col.ix[0:1,0:2],data_5row_2col.ix[0:1,[0,2]],选取部分行和列需加”[]”

3. 排序

代码如下:

print data_1row.sort_values()
print data_5row_2col.sort_values(by=u'燃料比')

测试结果如下:

顶温西北  173.00
顶温西南  184.00
顶温东南  184.00
顶温东北  188.00
燃料比   510.35
Name: 1, dtype: float64
   燃料比 顶温西南
1 510.35  184
5 511.24  174
3 511.51  190
4 531.02  180
0 531.46  185
2 533.49  180

4. 删除重复的行

代码如下:

print data_5row_2col[u'顶温西南'].drop_duplicates()#剔除重复行数据

测试结果如下:

0  185
1  184
2  180
3  190
5  174
Name: 顶温西南, dtype: int64

说明:从测试结果3中可以看出顶温西南index=2的数据与index=4的数据重复,测试结果4显示将index=4的顶温西南数据删除

相关推荐:

对Python 2.7 pandas 中的read_excel详解

python+pandas分析nginx日志的实例


The above is the detailed content of Methods of DataFrame type data operation functions in python pandas. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn