欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Pandas根據(jù)條件實(shí)現(xiàn)替換列中的值

 更新時間:2024年01月17日 10:06:40   作者:python收藏家  
在使用Pandas的Python中,DataFrame列中的值可以通過使用各種內(nèi)置函數(shù)根據(jù)條件進(jìn)行替換,本文主要來和大家討論在Pandas中用條件替換數(shù)據(jù)集列中的值的各種方法,希望對大家有所幫助

在使用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ù)值詳解

    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-07
  • Python  中的pass語句語法詳析

    Python  中的pass語句語法詳析

    這篇文章主要介紹了Python 中的pass語句語法詳析,pass是一種空操作(null operation),解釋器執(zhí)行到它的時候,除了檢查語法是否合法,什么也不做就直接跳過
    2022-07-07
  • python 一個figure上顯示多個圖像的實(shí)例

    python 一個figure上顯示多個圖像的實(shí)例

    今天小編就為大家分享一篇python 一個figure上顯示多個圖像的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • Pandas 透視表和交叉表的實(shí)現(xiàn)示例

    Pandas 透視表和交叉表的實(shí)現(xiàn)示例

    本文主要介紹了Pandas 透視表和交叉表的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-07-07
  • Python海龜繪圖之繪制趣味簡筆畫

    Python海龜繪圖之繪制趣味簡筆畫

    大家好,本篇文章主要講的是Python海龜繪圖之繪制趣味簡筆畫,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12
  • 淺述python2與python3的簡單區(qū)別

    淺述python2與python3的簡單區(qū)別

    python2:print語句,語句就意味著可以直接跟要打印的東西而python3:print函數(shù),函數(shù)就以為這必須要加上括號才能調(diào)用。下面通過本文給大家介紹python2與python3的簡單區(qū)別,感興趣的朋友跟隨小編一起看看吧
    2018-09-09
  • Python中axis=0與axis=1指的方向有什么不同詳解

    Python中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-01
  • Django 路由控制的實(shí)現(xiàn)代碼

    Django 路由控制的實(shí)現(xiàn)代碼

    這篇文章主要介紹了Django 路由控制的實(shí)現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-11-11
  • 基于pytorch的lstm參數(shù)使用詳解

    基于pytorch的lstm參數(shù)使用詳解

    今天小編就為大家分享一篇基于pytorch的lstm參數(shù)使用詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01
  • Python Requests模擬登錄實(shí)現(xiàn)圖書館座位自動預(yù)約

    Python Requests模擬登錄實(shí)現(xiàn)圖書館座位自動預(yù)約

    這篇文章主要為大家詳細(xì)介紹了Python Requests的模擬登錄,Python實(shí)現(xiàn)圖書館座位自動預(yù)約,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-04-04

最新評論