首页 > 其他分享 >pandas小技巧

pandas小技巧

时间:2023-03-10 15:57:47浏览次数:42  
标签:技巧 df res astype replace str pandas

1. 删除列

import pandas as pd
df.drop("Unnamed: 0", axis=1, inplace=True)

2. 转换列的格式

df["Date"] = df["Date"].astype("datetime64[ns]")
df = df.astype({'trans_time': 'datetime64[ns]', 'store_code'a:int, 'goods_id':int})

3. 数据膨胀

res_eff_gt3['weekday'] = [list(range(7))] * len(res_eff_gt3)
res_eff_gt3.explode('weekday')

4. 字符拆分与切分

df["Name"].str.split(",", expand=True)
# $19,100字符类型转成数值型
df["Payment"] = df["Payment"].str[1:].str.replace(",", ".").astype("float")

5. 正则:字符串替换

想要字母
df["Note"].str.replace('[^a-zA-Z]', '')
# 想要字母和数字
df["Note"].str.replace('[^a-zA-Z0-9]', '')

标签:技巧,df,res,astype,replace,str,pandas
From: https://www.cnblogs.com/tian1022/p/17203599.html

相关文章

  • 【python】pandas 时间序列转换
    1.时间戳-->时间time_stamp=1677895200000#2023-03-0410:00:00pd.to_datetime(time_stamp,unit='ms')#Timestamp('2023-03-0402:00:00')utc时间pd.to_dat......
  • Python - pandas 数据处理
    数据处理pandas数据读取pd.read_csv:csv/tsv/txt用逗号、tab分隔的纯文本文件pd.read_excel::微软xls或者xlsx文件pd.read_sql:mysql关系型数据库pd.rea......
  • OpenHarmony应用开发技巧 - 如何安装ServiceExtensionAbility
    概述文档环境开发环境:Windows11DevEcoStudio版本:DevEcoStudio3.1Beta1(3.1.0.200)SDK版本:3.2.10.7(OpenHarmony3.2Beta5FullSDK)应用模型:Stage开发板型......
  • OpenHarmony应用开发技巧 - 如何获取证书指纹
    概述文档环境开发环境:Windows11DevEcoStudio版本:DevEcoStudio3.1Beta1(3.1.0.200)SDK版本:3.2.10.7(OpenHarmony3.2Beta5FullSDK)应用模型:Stage开发板型号:DAYU2......
  • python pandas DataFrame, Series 为空的情况
      #!/usr/bin/evnpythonimportnumpyasnpimportpandasaspddf_empty=pd.DataFrame({"empty_index":[]})print("df_empty:",df_empty)ifdf_empty.em......
  • c++ 代码技巧
    数学运算性能大多数数据运算不存在性能问题,但是相对来说,整型的除法运算还是比较昂贵的。参考下面的例子:uint32_tBM_S1(uint64_tv){uint32_tresult=0;do{......
  • ctfshow web入门 命令执行 特征及绕过技巧
    远程命令执行(RemoteCommandExecution,RCE)原理命令执行漏洞是指服务器没有对执行的命令进行过滤,用户可以随意执行系统命令,命令执行漏洞属于高危漏洞之一。危险函数......
  • Pandas的索引,选择和定位
    该博客是Pandas课程习题,前往此处可学习课程该习题引用的数据集为WineReviewsdataset,前往此处下载数据集初始化importpandasaspdreviews=pd.read_csv("./winema......
  • EBS 开发技巧 常用代码
    Form开发技巧常用代码Form中的变量Form中用到的变量,总结如下:变量定义位置作用域,由低到高访问方法引用方式各层触发器中的变量该触发器FormPL/SQL变量......
  • MegEngine 使用小技巧:借助 DataLoader 获取分批数据
    在使用MegEngine进行模型训练时,首先要进行的是数据加载和预处理。在此过程中,MegEngine中的megengine.data模块,提供了数据分批功能,其内部实现流程如下图:通过使用Datal......