日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

python replace_DF.replace介绍

發(fā)布時(shí)間:2025/3/15 python 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python replace_DF.replace介绍 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

介紹

replace是python.pandas包下DataFrame中一個(gè)數(shù)據(jù)替換的方法。

用法

pandas.DataFrame.replace

DataFrame.replace(to_replace=None,value=None,inplace=False,limit=None,regex=False,method='pad')

替代范圍:

str, regex, list, dict, Series, int, float, or None

實(shí)例

數(shù)值取代

>>> s = pd.Series([0, 1, 2, 3, 4]) >>> s.replace(0, 5) 0 5 1 1 2 2 3 3 4 4 dtype: int64>>> df = pd.DataFrame({'A': [0, 1, 2, 3, 4], ... 'B': [5, 6, 7, 8, 9], ... 'C': ['a', 'b', 'c', 'd', 'e']}) >>> df.replace(0, 5)A B C 0 5 5 a 1 1 6 b 2 2 7 c 3 3 8 d 4 4 9 e

列表取代

>>> df.replace([0, 1, 2, 3], 4)A B C 0 4 5 a 1 4 6 b 2 4 7 c 3 4 8 d 4 4 9 e>>> df.replace([0, 1, 2, 3], [4, 3, 2, 1])A B C 0 4 5 a 1 3 6 b 2 2 7 c 3 1 8 d 4 4 9 e>>> s.replace([1, 2], method='bfill') 0 0 1 3 2 3 3 3 4 4 dtype: int64

字典取代

>>> df.replace({0: 10, 1: 100}) A B C 0 10 5 a 1 100 6 b 2 2 7 c 3 3 8 d 4 4 9 e df.replace({'A': {0: 100, 4: 400}})A B C 0 100 5 a 1 1 6 b 2 2 7 c 3 3 8 d 4 400 9 e

表達(dá)式取代

>>> df = pd.DataFrame({'A': ['bat', 'foo', 'bait'], ... 'B': ['abc', 'bar', 'xyz']}) >>> df.replace(to_replace=r'^ba.$', value='new', regex=True)A B 0 new abc 1 foo new 2 bait xyz>>> df.replace({'A': r'^ba.$'}, {'A': 'new'}, regex=True)A B 0 new abc 1 foo bar 2 bait xyz>>> df.replace(regex=r'^ba.$', value='new')A B 0 new abc 1 foo new 2 bait xyz>>> df.replace(regex={r'^ba.$': 'new', 'foo': 'xyz'})A B 0 new abc 1 xyz new 2 bait xyz>>> df.replace(regex=[r'^ba.$', 'foo'], value='new')A B 0 new abc 1 new new 2 bait xyz

詳情見(jiàn)鏈接:

pandas.DataFrame.replace - pandas 0.24.2 documentation?pandas.pydata.org

總結(jié)

以上是生活随笔為你收集整理的python replace_DF.replace介绍的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。