Làm cách nào để áp dụng một hàm cho một cột của khung dữ liệu trong python?

Khi làm việc với dữ liệu trong Pandas, chúng tôi thực hiện rất nhiều thao tác trên dữ liệu để lấy dữ liệu ở dạng mong muốn. Một trong những thao tác này có thể là chúng tôi muốn tạo các cột mới trong DataFrame dựa trên kết quả của một số thao tác trên các cột hiện có trong DataFrame. Hãy thảo luận về một số cách mà chúng ta có thể làm điều đó.  

Đưa ra một Dataframe chứa dữ liệu về một sự kiện, chúng tôi muốn tạo một cột mới có tên là 'Discounted_Price', được tính sau khi áp dụng chiết khấu 10% cho Giá vé

ví dụ 1. Chúng ta có thể sử dụng DataFrame. apply[] để đạt được nhiệm vụ này.  

Python3




# importing pandas as pd

import pandas as pd

 

# Creating the DataFrame

df_______5 pd.DataFrame[{'Date':['10/2/2011'import0import1import0import3import0import5import6

import7_______18:[pandas as pd0import0pandas as pd2import0pandas as pd4import0pandas as pd6import6

import7pandas as pd9:[# Creating the DataFrame1import0# Creating the DataFrame3import0# Creating the DataFrame5import0# Creating the DataFrame7# Creating the DataFrame8

 

# Creating the DataFrame9

df0df1

đầu ra

 

khung dữ liệu mẫu

Bây giờ, chúng tôi sẽ tạo một cột mới có tên là 'Discounted_Price' sau khi áp dụng chiết khấu 10% cho cột 'Chi phí' hiện có

Python3




df2

df3df4df5_______5 df7____48df9=0 =1=2

=3______54=5 =6=7= =9pd.DataFrame[{0

 

pd.DataFrame[{1

pd.DataFrame[{2

df0df1

đầu ra

 

Khung dữ liệu với cột mới được tạo

  

ví dụ 2. Chúng ta có thể đạt được kết quả tương tự bằng cách trực tiếp thực hiện thao tác được yêu cầu trên phần tử cột mong muốn.  

Python3




import pandas as pd

 

# Creating the DataFrame

df_______5 pd.DataFrame[{'Date':['10/2/2011'import0import1import0import3import0import5import6

import7_______18:[pandas as pd0import0pandas as pd2import0pandas as pd4import0pandas as pd6import6

import7pandas as pd9:[# Creating the DataFrame1import0# Creating the DataFrame3import0# Creating the DataFrame5import0# Creating the DataFrame7# Creating the DataFrame8

 

import03

import04

 

import05

df3df4df5 = df3pandas as pd9df5 =2 df9 _______56 =5 df3pandas as pd9import19

 

import20

import21

df0df1

đầu ra

Khung dữ liệu đầu ra

ví dụ 3. Sử dụng Khung dữ liệu. map[] chức năng để tạo cột mới từ cột hiện có bằng chức năng ánh xạ

Chúng tôi sẽ tạo một khung dữ liệu với một số dữ liệu mẫu

Python3




import24= import26

import27import28import29import30import0import32import0import34import0import36import0import38import0import40import0import42import0import44import6

import27import47import29# Creating the DataFrame1import0import51import0import53import0import55import0import57import0import59import0# Creating the DataFrame3import0import53df5

import65

import66

df= import69

import70

import71

đầu ra

khung dữ liệu mẫu

Bây giờ, chúng ta sẽ tạo một hàm ánh xạ [salary_stats] và sử dụng DataFrame. map[] để tạo một cột mới từ một cột hiện có

Python3




import72 import73

import27import75 import76# Creating the DataFrame1import78

import79import80 import81

import27import75 # Creating the DataFrame1 import85= import76import88import78

import79import80 import92

import27import94 import88 import85= import76import99import78

import79import80 pandas as pd03

import27import94 import99 import85= import76import53import78

import79import80 pandas as pd14

import27import94 pandas as pd17_______5 import53import78

import79import80 pandas as pd23

 

df3pandas as pd25df5= df3pandas as pd29pandas as pd30pandas as pd31pandas as pd32

import71

đầu ra

Xuất khung dữ liệu với cột mới

Giải trình. Ở đây chúng tôi đã sử dụng DataFrame của gấu trúc. map[] để ánh xạ từng giá trị thành một chuỗi dựa trên logic ánh xạ đã xác định của chúng tôi. Chuỗi giá trị kết quả được gán cho một cột mới, “salary_stats”

Áp dụng [] trong Pandas là gì?

Phương thức apply[] cho phép bạn áp dụng một hàm dọc theo một trong các trục của DataFrame, mặc định là 0, là trục chỉ mục [hàng].

Bạn sẽ áp dụng một hàm cho mọi thành phần dữ liệu trong DataFrame như thế nào?

Người ta có thể sử dụng hàm apply[] để áp dụng hàm cho mọi hàng trong khung dữ liệu đã cho.

Chủ Đề