Pandas根據(jù)條件實(shí)現(xiàn)替換列中的值
在使用Pandas的Python中,DataFrame列中的值可以通過使用各種內(nèi)置函數(shù)根據(jù)條件進(jìn)行替換。在本文中,我們將討論在Pandas中用條件替換數(shù)據(jù)集列中的值的各種方法。
1. 使用dataframe.loc方法
使用此方法,我們可以使用條件或布爾數(shù)組訪問一組行或列。如果我們可以訪問它,我們也可以操縱值,是的!這是我們的第一個方法,通過pandas中的dataframe.loc[]函數(shù),我們可以訪問一個列并使用條件更改其值。
語法: df.loc[ df[“column_name”] == “some_value”, “column_name”] = “value”
注意:您也可以使用其他運(yùn)算符來構(gòu)造條件以更改數(shù)值。
例子:在此示例中,代碼導(dǎo)入Pandas和NumPy庫,從保存學(xué)生數(shù)據(jù)的字典(‘Student’)構(gòu)建DataFrame(‘df’),然后在打印修改后的DataFrame之前將’gender’列的值從“male”更改為“1”。
# Importing the libraries import pandas as pd import numpy as np # data Student = { 'Name': ['John', 'Jay', 'sachin', 'Geetha', 'Amutha', 'ganesh'], 'gender': ['male', 'male', 'male', 'female', 'female', 'male'], 'math score': [50, 100, 70, 80, 75, 40], 'test preparation': ['none', 'completed', 'none', 'completed', 'completed', 'none'], } # creating a Dataframe object df = pd.DataFrame(Student) # Applying the condition df.loc[df["gender"] == "male", "gender"] = 1 print(df)
輸出
Name gender math score test preparation
0 John 1 50 none
1 Jay 1 100 completed
2 sachin 1 70 none
3 Geetha female 80 completed
4 Amutha female 75 completed
5 ganesh 1 40 none
2. 使用NumPy.where方法
我們將要看到的另一個方法是使用NumPy庫。NumPy是一個非常流行的庫,用于計(jì)算2D和3D數(shù)組。它為我們提供了一個非常有用的方法,where()可以訪問帶有條件的特定行或列。我們還可以使用此函數(shù)更改列的特定值。
語法: df[“column_name”] = np.where(df[“column_name”]==”some_value”, value_if_true, value_if_false)
例子:在此示例中,代碼導(dǎo)入Pandas和NumPy庫,從包含學(xué)生數(shù)據(jù)的名為“student”的字典中構(gòu)建名為“df”的DataFrame,并使用NumPy np.where函數(shù)將“gender”列的值從“female”更改為“0”,將“male”更改為1。然后輸出更改后的DataFrame。
# Importing the libraries import pandas as pd import numpy as np # data student = { 'Name': ['John', 'Jay', 'sachin', 'Geetha', 'Amutha', 'ganesh'], 'gender': ['male', 'male', 'male', 'female', 'female', 'male'], 'math score': [50, 100, 70, 80, 75, 40], 'test preparation': ['none', 'completed', 'none', 'completed', 'completed', 'none'], } # creating a Dataframe object df = pd.DataFrame(student) # Applying the condition df["gender"] = np.where(df["gender"] == "female", 0, 1) print(df)
輸出
Name gender math score test preparation
0 John 1 50 none
1 Jay 1 100 completed
2 sachin 1 70 none
3 Geetha 0 80 completed
4 Amutha 0 75 completed
5 ganesh 1 40 none
3. 使用mask方法
Pandas masking函數(shù)用于將任何行或列的值替換為條件。
語法: df[‘column_name’].mask( df[‘column_name’] == ‘some_value’, value , inplace=True )
例子:在此示例中,代碼導(dǎo)入Pandas和NumPy庫,從包含學(xué)生數(shù)據(jù)的名為“student”的字典中構(gòu)建名為“df”的DataFrame,然后使用Pandas mask函數(shù)將“gender”列中的值“female”替換為0,然后打印修改后的DataFrame。它還包括一行注釋,顯示如何有條件地將“math score”列中的值替換為“good”(對于大于或等于60的分?jǐn)?shù))。
# Importing the libraries import pandas as pd import numpy as np # data student = { 'Name': ['John', 'Jay', 'sachin', 'Geetha', 'Amutha', 'ganesh'], 'gender': ['male', 'male', 'male', 'female', 'female', 'male'], 'math score': [50, 100, 70, 80, 75, 40], 'test preparation': ['none', 'completed', 'none', 'completed', 'completed', 'none'], } # creating a Dataframe object df = pd.DataFrame(student) # Applying the condition df['gender'].mask(df['gender'] == 'female', 0, inplace=True) print(df) # Try this too #df['math score'].mask(df['math score'] >=60 ,'good', inplace=True)
輸出
Name gender math score test preparation
0 John male 50 none
1 Jay male 100 completed
2 sachin male 70 none
3 Geetha 0 80 completed
4 Amutha 0 75 completed
5 ganesh male 40 none
4. 使用apply()和lambda函數(shù)
在這個例子中,我們使用了lamda和apply()函數(shù)來根據(jù)條件替換列中的值。
# Importing the libraries import pandas as pd import numpy as np # Data student = { 'Name': ['John', 'Jay', 'sachin', 'Geetha', 'Amutha', 'ganesh'], 'gender': ['male', 'male', 'male', 'female', 'female', 'male'], 'math score': [50, 100, 70, 80, 75, 40], 'test preparation': ['none', 'completed', 'none', 'completed', 'completed', 'none'], } # Creating a DataFrame object df = pd.DataFrame(student) # Applying the condition using apply and lambda df['gender'] = df['gender'].apply(lambda x: 0 if x == 'female' else x) print(df)
輸出
Name gender math score test preparation
0 John male 50 none
1 Jay male 100 completed
2 sachin male 70 none
3 Geetha 0 80 completed
4 Amutha 0 75 completed
5 ganesh male 40 none
到此這篇關(guān)于Pandas根據(jù)條件實(shí)現(xiàn)替換列中的值的文章就介紹到這了,更多相關(guān)Pandas替換列值內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python?Pandas中DataFrame.drop_duplicates()刪除重復(fù)值詳解
在實(shí)際處理數(shù)據(jù)中,數(shù)據(jù)預(yù)處理操作中,常常需要去除掉重復(fù)的數(shù)據(jù),這篇文章主要給大家介紹了關(guān)于Python?Pandas中DataFrame.drop_duplicates()刪除重復(fù)值的相關(guān)資料,需要的朋友可以參考下2022-07-07python 一個figure上顯示多個圖像的實(shí)例
今天小編就為大家分享一篇python 一個figure上顯示多個圖像的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07Pandas 透視表和交叉表的實(shí)現(xiàn)示例
本文主要介紹了Pandas 透視表和交叉表的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07Python中axis=0與axis=1指的方向有什么不同詳解
對數(shù)據(jù)進(jìn)行操作時,經(jīng)常需要在橫軸方向或者數(shù)軸方向?qū)?shù)據(jù)進(jìn)行操作,這時需要設(shè)定參數(shù)axis的值,下面這篇文章主要給大家介紹了關(guān)于Python中axis=0與axis=1指的方向有什么不同的相關(guān)資料,需要的朋友可以參考下2024-01-01Python Requests模擬登錄實(shí)現(xiàn)圖書館座位自動預(yù)約
這篇文章主要為大家詳細(xì)介紹了Python Requests的模擬登錄,Python實(shí)現(xiàn)圖書館座位自動預(yù)約,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04