用apply处理pandas比用for循环,快了无数倍,测试如下:

我们有一个pandas加载的dataframe如下,features是0和1特征的组合,可惜都是str形式(字符串形式),我们要将其转换成一个装有整型int 0和1的list

(1)用for循坏(耗时约3小时)

1 from tqdm import tqdm #计时器函数
2 for i in tqdm(range(df.shape[0])):
3 df['features'][i] = df['features'][i].split(",") #每一行形如0,0,1,1,0,1,1的string,所以按照逗号切割,返回一个list
4 for j in range(len(df['features'][i])): #遍历该list,对于每个元素进行int转换
5 df['features'][i][j] = int(df['features'][i][j])
6
7 print(type(df['features'][0]))

(2)推荐用apply方法(耗时约30秒)

 1 from time import time
2 from tqdm import tqdm
3
4 def func(x):
5 l = x.split(",")
6 for i in range(len(l)):
7 l[i] = int(l[i])
8 return l
9
10 stime = time()
11 df['new_features'] = df['features'].apply(func)
12 endtime = time()
13
14 print("time:"+str(endtime-stime)+"s")
15 #df.head()
16 print("over")

python——pandas技巧(处理dataframe每个元素,不用for,而用apply)的更多相关文章

  1. python数据分析之pandas库的DataFrame应用二

    本节介绍Series和DataFrame中的数据的基本手段 重新索引 pandas对象的一个重要方法就是reindex,作用是创建一个适应新索引的新对象 ''' Created on 2016-8-1 ...

  2. [转]python中pandas库中DataFrame对行和列的操作使用方法

    转自:http://blog.csdn.net/u011089523/article/details/60341016 用pandas中的DataFrame时选取行或列: import numpy a ...

  3. python. pandas(series,dataframe,index) method test

    python. pandas(series,dataframe,index,reindex,csv file read and write) method test import pandas as ...

  4. oracle数据据 Python+Pandas 获取Oracle数据库并加入DataFrame

    import pandas as pd import sys import imp imp.reload(sys) from sqlalchemy import create_engine impor ...

  5. 【跟着stackoverflow学Pandas】 - Adding new column to existing DataFrame in Python pandas - Pandas 添加列

    最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...

  6. python pandas.Series&&DataFrame&& set_index&reset_index

    参考CookBook :http://pandas.pydata.org/pandas-docs/stable/cookbook.html Pandas set_index&reset_ind ...

  7. python中pandas库中DataFrame对行和列的操作使用方法

    用pandas中的DataFrame时选取行或列: import numpy as np import pandas as pd from pandas import Sereis, DataFram ...

  8. python pandas ---Series,DataFrame 创建方法,操作运算操作(赋值,sort,get,del,pop,insert,+,-,*,/)

    pandas 是基于 Numpy 构建的含有更高级数据结构和工具的数据分析包 pandas 也是围绕着 Series 和 DataFrame 两个核心数据结构展开的, 导入如下: from panda ...

  9. Python Pandas -- DataFrame

    pandas.DataFrame class pandas.DataFrame(data=None, index=None, columns=None, dtype=None, copy=False) ...

随机推荐

  1. @requestBody 与@requestparam详解

    @RequestParam注解接收的参数是来自于requestHeader中,即请求头.都是用来获取请求路径url 中的动态参数,格式为xxx?username=123&password=45 ...

  2. shell 脚本之set 命令(转)

    服务器的开发和管理离不开 Bash 脚本,掌握它需要学习大量的细节. set命令是 Bash 脚本的重要环节,却常常被忽视,导致脚本的安全性和可维护性出问题.本文介绍它的基本用法,让你可以更安心地使用 ...

  3. iscsi一致性的测试验证方法

    前言 如果使用场景是多路径iscsi,那么数据一致性的就需要去验证一致性,就需要一个比较通用的测试方法,最近在处理这块,记录下简单的测试方法 测试方法 写入数据 dd if=/dev/urandom ...

  4. 如何通过iptables代理访问内网

    场景 A机器能够联通内网机器,B机器能够联通A机器,但是访问不到内网机器,场景是希望通过A机器能够转发直接联通局域网内的其它机器 机器IP 内网为172.0.0.x/24 A机器为172.0.0.10 ...

  5. css类名的使用

    ` Document .box { color: rgba(255, 0, 0, 1) } .box .box2 { color: rgba(0, 128, 0, 1) } .box.box2 { c ...

  6. ESP8266 鼓捣记 - 从零制作一个温湿度计

    一.前言 经过上一篇文章 <ESP8266 鼓捣记 - 入门(环境搭建) >搭建好环境后,肯定不会满足于 Hello World ,想快速做一个实际有用的东西出来,我认为温湿度计就非常合适 ...

  7. Monitor的扩展支持string的超时锁

    对Monitor的使用可以防止lock的时间过长并且可以设置其对应的超时时间达到对预期代码的一个控制,合理的使用timeout可以有助于程序的健壮性.但是对于不同的并发程序可能某些时候我们需要的粒度是 ...

  8. 如何用思维导图软件MindManager制作项目管理图表

    项目管理的官方解释为:运用各种相关技能.方法与工具,为满足或超越项目有关各方对项目的要求与期望,所开展的各种计划.组织.领导.控制等方面的活动. 其实使用MindManager思维导图软件来创建项目管 ...

  9. python 工业日志模块 未来的python日志最佳实践

    目录 介绍 好的功能 安装方法 参数介绍 呆log 参数与 使用方法 版本说明 后期版本规划 todo 感谢 介绍 呆log:工业中,python日志模块,安装即用.理论上支持 python2, py ...

  10. Java之 循环(三)

    1. switch语句 1.1 分支语句switch语句 格式 switch (表达式) { case 1: 语句体1; break; case 2: 语句体2; break; ... default ...