python——pandas技巧(处理dataframe每个元素,不用for,而用apply)
用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)的更多相关文章
- python数据分析之pandas库的DataFrame应用二
本节介绍Series和DataFrame中的数据的基本手段 重新索引 pandas对象的一个重要方法就是reindex,作用是创建一个适应新索引的新对象 ''' Created on 2016-8-1 ...
- [转]python中pandas库中DataFrame对行和列的操作使用方法
转自:http://blog.csdn.net/u011089523/article/details/60341016 用pandas中的DataFrame时选取行或列: import numpy a ...
- python. pandas(series,dataframe,index) method test
python. pandas(series,dataframe,index,reindex,csv file read and write) method test import pandas as ...
- oracle数据据 Python+Pandas 获取Oracle数据库并加入DataFrame
import pandas as pd import sys import imp imp.reload(sys) from sqlalchemy import create_engine impor ...
- 【跟着stackoverflow学Pandas】 - Adding new column to existing DataFrame in Python pandas - Pandas 添加列
最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...
- python pandas.Series&&DataFrame&& set_index&reset_index
参考CookBook :http://pandas.pydata.org/pandas-docs/stable/cookbook.html Pandas set_index&reset_ind ...
- python中pandas库中DataFrame对行和列的操作使用方法
用pandas中的DataFrame时选取行或列: import numpy as np import pandas as pd from pandas import Sereis, DataFram ...
- python pandas ---Series,DataFrame 创建方法,操作运算操作(赋值,sort,get,del,pop,insert,+,-,*,/)
pandas 是基于 Numpy 构建的含有更高级数据结构和工具的数据分析包 pandas 也是围绕着 Series 和 DataFrame 两个核心数据结构展开的, 导入如下: from panda ...
- Python Pandas -- DataFrame
pandas.DataFrame class pandas.DataFrame(data=None, index=None, columns=None, dtype=None, copy=False) ...
随机推荐
- Apache Shiro (Shiro-550)(cve_2016_4437)远程代码执行 - 漏洞复现
0x00 漏洞原理 Apache Shiro框架提供了记住密码的功能(RememberMe),用户登录成功后会生成经过加密并编码的cookie.在服务端对rememberMe的cookie值, 先ba ...
- python之 《pandas》
pandas稍微比numpy处理数据起来还是要慢一点,pandas呢是numpy的升级版,可以说各有所长,numpy的优势是用来处理矩阵,而pandas的优势是处理数表. 1. Series 线性数表 ...
- 关于mybatisPlus一些坑,当条件为null时
1.TStaffDepart 属性有值是才匹配条件,会报错,相当于mybatis if 判断 eg:TStaffDepart staffDepart = new TStaffDepart();staf ...
- python学习--sys.argv
sys.argv是获取命令行参数的: sys.argv[0]表示代码本身文件路径:从1开始获取参数. import sysprint (sys.argv[0])count = int(sys.argv ...
- HBuilderX SVN地址更改(SVN服务器IP地址变更)
HBuilderX编辑器中无法修改SVN地址,需要手动在SVN工具中修改 修改步骤: 1.右键编辑器中的SVN项目,选择打开文件所在目录 2.目录中空白处右键,选择TortoiseSVN --> ...
- .Net orm 开源项目 FreeSql 2.0.0(满意的答卷)
写在开头 2018年11月头脑发热到今天,一晃已经两年,当初从舒服区走向一个巨大的坑,回头一看后背一凉. 两年时间从无到有,经历数不清的日夜奋斗(有人问花了多长时间投入,答案:全职x2 + 两年无休息 ...
- Edison:FL Studio中的常用音频录制与剪辑插件
Edison是FL Studio中的一个完全集成的音频编辑和录制工具.Edison加载到效果插槽(在任何调音台音轨中),然后录制或播放该位置的音频.您可以在任意数量的混音器轨道或效果插槽中根据需要加载 ...
- kube-flannel.yml 文件
---kind: ClusterRoleapiVersion: rbac.authorization.k8s.io/v1beta1metadata: name: flannelrules: - api ...
- Linux中influx数据库进程杀不掉,父进程为1
influx数据库一直杀不掉,父进程为1是个僵尸进程 后来我才发现,influx是运行运行状态 我只需要使用命令,停掉influx即可停止改进程
- Mybatis【2.2】-- Mybatis关于创建SqlSession源码分析的几点疑问?
代码直接放在Github仓库[https://github.com/Damaer/Mybatis-Learning ],可直接运行,就不占篇幅了. 目录 1.为什么我们使用SQLSessionFact ...