当前位置:网站首页>dataframe 常用操作
dataframe 常用操作
2022-08-05 05:15:00 【sc0024】
1、读取csv
import pandas as pd
df=pd.read_csv('/data1/littlesc/Uplift/criteo-uplift-v2.1.csv')
2、返回数据的行数
df.shape[0]
3、给某一列/某些列改名
df = df.rename(columns={
'treatment':'treatment_label'})
4、筛选
df_t = df[df['treatment_label']==1]
5、划分训练集和验证集
from sklearn.model_selection import train_test_split
df_train, df_test = train_test_split(df_use, test_size=0.3, random_state=111)
6、把两个列名一样的df竖直拼接起来
df = df_c.append(df_t_use, ignore_index=True)
7、reset_index
df_use=df_use.reset_index(drop=True)
8、去掉某一列或某些列
df_train=df_train.drop(columns=['treatment_group_key'])
9、将数值型转为字符串
df_train['upliftScore'] = df_train['upliftScore'].apply(str)
10、将字符型转成数值型
matched["upliftScore"] = pd.to_numeric(matched["upliftScore"],errors='coerce')
10.5 float转int
df['Class'] = df['Class'].astype(int)
11、获取dataframe所有的列名
方法一:[column for column in df]
方法二:list(df)
12、获取列的数据类型
每一列:df.dtypes
某一列:df['id'].dtypes
13、缺失值处理
#删除nan行
df=df.dropna()
# 删除nan列
df=df.dropna(axis=1)
#这一行都是nan才删除
df=df.dropna(how='all')
#一行中有一个nan就删除这一行
df_use=df_use.dropna(axis=0,how='any')
# 将nan设置为0
df.fillna(0)
#使用下一行的值进行填充
df=df.fillna(method='bfill')
#使用上一行的值进行填充
df=df.fillna(method='ffill')
#删除gender这一列是NaN的数据
data=data.dropna(subset=['gender'])
#筛选gender是'М'或'Ж'的数据
gender = ['М', 'Ж']
genders = '|'.join(gender)
data = data[data['gender'].str.contains(genders)]
#查看含有nan的行
df[df.isna().any(axis=1)]
#查看不含有nan的行
df[~df.isna().any(axis=1)]
14、解除行列显示限制
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
15、随机采样
DataFrame.sample(n, frac, replace, weights, random_state, axis)
n: 要抽取的行数,需为整数值
frac:抽取的比列,需为小数值,比方说我们想随机抽取30%的数据,则设置frac=0.3即可。
replace:抽样后的数据是否代替原DataFrame(),默认为False
weights:默认为等概率加权
random_state:随机种子,本质是一个控制器,设置此值为任意实数,则每次随机的结果是一样的
axis:抽取数据的行还是列,axis=0的时是抽取行,axis=1时是抽取列
边栏推荐
- 【Pytorch学习笔记】8.训练类别不均衡数据时,如何使用WeightedRandomSampler(权重采样器)
- [Pytorch study notes] 9. How to evaluate the classification results of the classifier - using confusion matrix, F1-score, ROC curve, PR curve, etc. (taking Softmax binary classification as an example)
- ES6 新特性:Class 的继承
- Day1:用原生JS把你的设备变成一台架子鼓!
- Service
- ECCV2022 | RU & Google propose zero-shot object detection with CLIP!
- 华科提出首个用于伪装实例分割的一阶段框架OSFormer
- flink部署操作-flink standalone集群安装部署
- 门徒Disciples体系:致力于成为“DAO世界”中的集大成者。
- 【论文阅读-表情捕捉】ExpNet: Landmark-Free, Deep, 3D Facial Expressions
猜你喜欢

MaskDistill-不需要标注数据的语义分割

It turns out that the MAE proposed by He Yuming is still a kind of data enhancement
![[Pytorch study notes] 9. How to evaluate the classification results of the classifier - using confusion matrix, F1-score, ROC curve, PR curve, etc. (taking Softmax binary classification as an example)](/img/ac/884d8aba8b9d363e3b9ae6de33d5a4.png)
[Pytorch study notes] 9. How to evaluate the classification results of the classifier - using confusion matrix, F1-score, ROC curve, PR curve, etc. (taking Softmax binary classification as an example)

Thread handler handle IntentServvice handlerThread

AWS 常用服务
![[Over 17] Pytorch rewrites keras](/img/a2/7f0c7eebd119373bf20c44de9f7947.png)
[Over 17] Pytorch rewrites keras

华科提出首个用于伪装实例分割的一阶段框架OSFormer

Flink Table API 和 SQL之概述

ECCV2022 | RU & Google propose zero-shot object detection with CLIP!

解决:Unknown column ‘id‘ in ‘where clause‘ 问题
随机推荐
如何编写一个优雅的Shell脚本(二)
【MySQL】数据库多表链接的查询方式
Service
【数据库和SQL学习笔记】4.SELECT查询2:排序(ORDER BY)、聚合函数、分组查询(GROUP BY)
[Go through 10] sklearn usage record
day8字典作业
学习总结day5
[Skill] Long-term update
【Reading】Long-term update
SQL(1) - Add, delete, modify and search
Mysql-连接https域名的Mysql数据源踩的坑
flink实例开发-batch批处理实例
Map、WeakMap
[Database and SQL study notes] 8. Views in SQL
哥廷根大学提出CLIPSeg,能同时作三个分割任务的模型
学习总结week2_4
Flink Table API 和 SQL之概述
【22李宏毅机器学习】课程大纲概述
初识机器学习
[Go through 11] Random Forest and Feature Engineering