pandas replace函数使用小结
来源:互联网 发布:淘宝网毛呢女装中长款 编辑:程序博客网 时间:2024/06/08 07:32
语法:replace(self, to_replace=None, value=None, inplace=False, limit=None, regex=False, method='pad', axis=None)
使用方法如下:
import numpy as npimport pandas as pddf = pd.read_csv('emp.csv')df
#Series对象值替换s = df.iloc[2]#获取行索引为2数据#单值替换s.replace('?',np.nan)#用np.nan替换?s.replace({'?':'NA'})#用NA替换?#多值替换s.replace(['?',r'$'],[np.nan,'NA'])#列表值替换s.replace({'?':np.nan,'$':'NA'})#字典映射#同缺失值填充方法类似s.replace(['?','$'],method='pad')#向前填充s.replace(['?','$'],method='ffill')#向前填充s.replace(['?','$'],method='bfill')#向后填充#limit参数控制填充次数s.replace(['?','$'],method='bfill',limit=1)#DataFrame对象值替换#单值替换df.replace('?',np.nan)#用np.nan替换?df.replace({'?':'NA'})#用NA替换?#按列指定单值替换df.replace({'EMPNO':'?'},np.nan)#用np.nan替换EMPNO列中?df.replace({'EMPNO':'?','ENAME':'.'},np.nan)#用np.nan替换EMPNO列中?和ENAME中.#多值替换df.replace(['?','.','$'],[np.nan,'NA','None'])##用np.nan替换?用NA替换. 用None替换$df.replace({'?':'NA','$':None})#用NA替换? 用None替换$df.replace({'?','$'},{'NA',None})#用NA替换? 用None替换$#正则替换df.replace(r'\?|\.|\$',np.nan,regex=True)#用np.nan替换?或.或$原字符df.replace([r'\?',r'\$'],np.nan,regex=True)#用np.nan替换?和$df.replace([r'\?',r'\$'],[np.nan,'NA'],regex=True)#用np.nan替换?用NA替换$符号df.replace(regex={r'\?':None})#value参数显示传递df.replace(regex=[r'\?|\.|\$'],value=np.nan)#用np.nan替换?或.或$原字符
阅读全文
0 0
- pandas replace函数使用小结
- Pandas.Dataframe使用小结
- pandas使用小结
- Pandas使用经验小结
- Oracle-Replace使用小结
- 近期使用的pandas小结
- Asp Replace 函数的使用
- sql CHARINDEX,REPLACE函数使用
- Pandas 文本数据方法 replace( )
- 【Python】Pandas的apply函数使用示例
- Pandas——DataFrame函数使用
- Python】Pandas的apply函数使用示例
- Sql Server REPLACE函数的使用
- 使用mysql的replace函数替换字符串
- 使用mysql的replace函数替换字符串
- JavaScript中replace()函数的使用
- 使用mysql的replace函数替换字符串
- Sql Server REPLACE函数的使用
- Redis Sentinel基本介绍(翻译以及总结)
- JAVA_基础之面向对象的概念
- GridView实现checkbox效果(单选效果)
- C语言函数的参数能允许几个
- Miktex 添加宏包
- pandas replace函数使用小结
- 《UNIX网络编程 卷1》 笔记: I/O复用 select函数
- TabLayout踩坑之IllegalArgumentException: Tab belongs to a different TabLayout.
- IMWeb提升营Day1 | 训练题6: 旋转数组的最小数字
- Access restriction: The type JPEGImageEncoder is not accessible due to restriction
- [LeetCode]AddTwoNumbers
- 信息安全概论
- 实训周五
- objdump选项说明