用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. python菜鸟教程学习1:背景性学习

    https://www.runoob.com/python3/python3-intro.html 优点 简单 -- Python 是一种代表简单主义思想的语言.阅读一个良好的 Python 程序就感 ...

  2. 性能问题eg

    线上问题 ./pidstat -w Linux 3.6.5-Broadcom Linux ((none)) 03/21/20 _armv7l_ (1 CPU) 15:04:17 UID PID csw ...

  3. fcntl函数用法——设置文件锁

    fcntl函数.锁定文件,设置文件锁.设置获取文件锁:F_GETLK .F_SETLK  .F_SETLKW文件锁结构,设置好用于fcntl函数的第三个参数.struct flock{    shor ...

  4. Distributing Custom Apps

    Distributing Custom Apps 分配自定义应用程序 November 10, 2020 2020年11月10日 Custom apps let you meet the unique ...

  5. python-网络安全编程第十天(web目录扫描&&fake_useragent模块&&optionParser模块)

    前言 昨天的内容没有完成今天花了点时间继续完成了 感觉自己的学习效率太低了!想办法提高学习效率吧 嗯 ,再制定下今天的目标 开始健身. python fake_useragent模块 1.UserAg ...

  6. 推荐一个适用于SpringBoot项目的轻量级HTTP客户端框架,快来试试它!

    在SpringBoot项目直接使用okhttp.httpClient或者RestTemplate发起HTTP请求,既繁琐又不方便统一管理.因此,在这里推荐一个适用于SpringBoot项目的轻量级HT ...

  7. 本地VM安装虚拟机,使用xshell连接

    首先把VM设置成上面那样 在ubuntu里面安装ssh apt-get install openssh-server 启动服务 /etc/init.d/ssh startifconfig 查看ip x ...

  8. Gradle全局代理配置

    配置文件路径:C:\Users\myName\.gradle\gradle.properties 代理配置内容: systemProp.http.proxyHost=127.0.0.1 systemP ...

  9. 【linux】i2c使用分析&源码实战

    目录 前言 1. 设备检查命令 1.1 查看I2C驱动 1.2 i2c-tools 1.2.1 I2C-detect安装 1.2.2 i2cdetect 命令 1.2.3 i2cget 命令 1.2. ...

  10. Java数据结构(十二)—— 霍夫曼树及霍夫曼编码

    霍夫曼树 基本介绍和创建 基本介绍 又称哈夫曼树,赫夫曼树 给定n个权值作为n个叶子节点,构造一棵二叉树,若该树的带权路径长度(wpl)达到最小,称为最优二叉树 霍夫曼树是带权路径长度最短的树,权值较 ...