Sử dụng một numpy. dtype hoặc kiểu Python để chuyển toàn bộ đối tượng pandas thành cùng loại. Ngoài ra, sử dụng {col. dtype, …}, trong đó col là nhãn cột và dtype là numpy. dtype hoặc loại Python để truyền một hoặc nhiều cột của DataFrame thành các loại dành riêng cho cột
bản sao bool, mặc định ĐúngTrả lại một bản sao khi
print [interviews.dtypes]
3 [hãy rất cẩn thận khi đặt >>> df.astype['int32'].dtypes col1 int32 col2 int32 dtype: object0 vì các thay đổi đối với giá trị sau đó có thể lan truyền sang các đối tượng gấu trúc khác]
Là một phần của việc sắp xếp dữ liệu của bạn, bạn có thể cần phải chuyển một cột Pandas DataFrame thành kiểu dữ liệu số nguyên
Chúng tôi sẽ tiếp tục và xem xét ba trường hợp chính
- Truyền một cột cụ thể từ float sang int
- Chuyển đổi một cột chứa các giá trị trống nan thành int
- Chuyển đổi nhiều cột thành int/int64
Tạo một khung dữ liệu Pandas
Hãy bắt đầu bằng cách viết một số mã Python đơn giản sẽ giúp chúng tôi tạo một số dữ liệu thử nghiệm mà bạn có thể sử dụng để theo dõi
import pandas pd
#Lists containing test data
offices = ['Paris', 'Madrid', 'London', 'Barcelona', 'Brussels']
num_interviews = [129.0, 132.0, 145.0, 230.0, pd.NA]
positions = [12.0, 15.0, 13.0, 13.5, 3]
#Create DataFrame from dictionary
interviews_dict = dict[office=offices, total_interviews =num_interviews, total_positions = positions ]
interviews = pd.DataFrame[interviews_dict]
interviews.head[]
print[interviews.head[]]
Đây là DataFrame của chúng tôi
officetotal_interviewstotal_positions0Paris129.012.01Madrid132.015.02London145.013.03Barcelona230.013.54Brussels3.0Hãy cùng tìm hiểu các kiểu dữ liệu cho các cột DataFrame khác nhau
print [interviews.dtypes]
office object
total_interviews object
total_positions float64
dtype: object
Chuyển đổi một cột từ float thành số nguyên
Chúng tôi sẽ bắt đầu bằng cách chuyển đổi một cột từ kiểu dữ liệu float64 sang int và int64
interviews['total_positions'].astype['int']
Điều này sẽ trả về một chuỗi truyền tới int. Để thay đổi loại thành int64, chỉ cần gõ
interviews['total_positions'].astype['int64']
Xử lý chuyển đổi cột thành int với giá trị nan
Bạn có thể đã lưu ý rằng một trong các cột DataFrame của chúng tôi chứa giá trị trống. Cố gắng truyền nó thành số nguyên sẽ hiển thị lỗi sau
TypeError: int[] argument must be a string, a bytes-like object or a number, not 'NAType'
Do đó, chúng ta nên xử lý các giá trị trống trước rồi tiếp tục và chuyển cột
interviews['total_interviews'].fillna[0,
inplace=True].astype[int]
Chuyển đổi nhiều cột thành kiểu int
Chúng ta hãy xem xét một kịch bản thực tế hơn trong đó chúng ta truyền nhiều cột cùng một lúc. Trước tiên, chúng tôi sẽ tiếp tục và xử lý các ô chứa giá trị trống
________số 8Sau đó, chúng tôi sẽ chuyển nhiều cột thành int64. Không giống như trước đây, chúng tôi sẽ chuyển một từ điển chứa các cột để chuyển đổi và loại dtype cần thiết cho mỗi
interviews_2 = interviews.astype[{'total_interviews':'int64', 'total_positions':'int64'}]
Chúng tôi sẽ kết thúc bằng cách xác minh các loại dữ liệu
import pandas pd
#Lists containing test data
offices = ['Paris', 'Madrid', 'London', 'Barcelona', 'Brussels']
num_interviews = [129.0, 132.0, 145.0, 230.0, pd.NA]
positions = [12.0, 15.0, 13.0, 13.5, 3]
#Create DataFrame from dictionary
interviews_dict = dict[office=offices, total_interviews =num_interviews, total_positions = positions ]
interviews = pd.DataFrame[interviews_dict]
interviews.head[]
print[interviews.head[]]
0Điều này sẽ kết xuất
print [interviews.dtypes]
0Đổi tên các cột đã chuyển đổi
Cuối cùng, chúng ta có thể tiếp tục và đổi tên các cột mà bạn vừa chuyển đổi. Cũng ở đây. chúng tôi sẽ chuyển một từ điển ánh xạ làm tham số cho phương thức DataFrame. Đây là một đoạn ngắn
Thay vì chỉ định chuyển đổi thành số nguyên theo từng cột, bạn có thể thực hiện thay vào đó ở cấp DataFrame bằng cách sử dụng
import pandas pd
#Lists containing test data
offices = ['Paris', 'Madrid', 'London', 'Barcelona', 'Brussels']
num_interviews = [129.0, 132.0, 145.0, 230.0, pd.NA]
positions = [12.0, 15.0, 13.0, 13.5, 3]
#Create DataFrame from dictionary
interviews_dict = dict[office=offices, total_interviews =num_interviews, total_positions = positions ]
interviews = pd.DataFrame[interviews_dict]
interviews.head[]
print[interviews.head[]]
20Ví dụ: hãy tạo một DataFrame mới với hai cột chỉ chứa số float
import pandas pd
#Lists containing test data
offices = ['Paris', 'Madrid', 'London', 'Barcelona', 'Brussels']
num_interviews = [129.0, 132.0, 145.0, 230.0, pd.NA]
positions = [12.0, 15.0, 13.0, 13.5, 3]
#Create DataFrame from dictionary
interviews_dict = dict[office=offices, total_interviews =num_interviews, total_positions = positions ]
interviews = pd.DataFrame[interviews_dict]
interviews.head[]
print[interviews.head[]]
21Bây giờ bạn sẽ nhận được Khung dữ liệu này với hai cột nổi
import pandas pd
#Lists containing test data
offices = ['Paris', 'Madrid', 'London', 'Barcelona', 'Brussels']
num_interviews = [129.0, 132.0, 145.0, 230.0, pd.NA]
positions = [12.0, 15.0, 13.0, 13.5, 3]
#Create DataFrame from dictionary
interviews_dict = dict[office=offices, total_interviews =num_interviews, total_positions = positions ]
interviews = pd.DataFrame[interviews_dict]
interviews.head[]
print[interviews.head[]]
22Để chuyển đổi số float thành số nguyên trong toàn bộ DataFrame, bạn sẽ cần thêm df = df. astype[int] vào mã
import pandas pd
#Lists containing test data
offices = ['Paris', 'Madrid', 'London', 'Barcelona', 'Brussels']
num_interviews = [129.0, 132.0, 145.0, 230.0, pd.NA]
positions = [12.0, 15.0, 13.0, 13.5, 3]
#Create DataFrame from dictionary
interviews_dict = dict[office=offices, total_interviews =num_interviews, total_positions = positions ]
interviews = pd.DataFrame[interviews_dict]
interviews.head[]
print[interviews.head[]]
23Như bạn có thể thấy, tất cả các cột trong DataFrame hiện được chuyển đổi thành số nguyên
import pandas pd
#Lists containing test data
offices = ['Paris', 'Madrid', 'London', 'Barcelona', 'Brussels']
num_interviews = [129.0, 132.0, 145.0, 230.0, pd.NA]
positions = [12.0, 15.0, 13.0, 13.5, 3]
#Create DataFrame from dictionary
interviews_dict = dict[office=offices, total_interviews =num_interviews, total_positions = positions ]
interviews = pd.DataFrame[interviews_dict]
interviews.head[]
print[interviews.head[]]
24Lưu ý rằng cách tiếp cận trên sẽ chỉ hoạt động nếu tất cả các cột trong DataFrame có kiểu dữ liệu là float
Điều gì sẽ xảy ra nếu bạn có một DataFrame hỗn hợp trong đó kiểu dữ liệu của một số [nhưng không phải tất cả] cột là float?
Phần dưới đây đề cập đến tình huống này
[3] Chuyển đổi DataFrame hỗn hợp trong đó kiểu dữ liệu của một số cột là float
Bây giờ, hãy tạo một DataFrame mới với 3 cột, trong đó 2 cột đầu tiên sẽ chứa các giá trị float, trong khi cột thứ ba sẽ chỉ bao gồm các chuỗi
import pandas pd
#Lists containing test data
offices = ['Paris', 'Madrid', 'London', 'Barcelona', 'Brussels']
num_interviews = [129.0, 132.0, 145.0, 230.0, pd.NA]
positions = [12.0, 15.0, 13.0, 13.5, 3]
#Create DataFrame from dictionary
interviews_dict = dict[office=offices, total_interviews =num_interviews, total_positions = positions ]
interviews = pd.DataFrame[interviews_dict]
interviews.head[]
print[interviews.head[]]
25Đây là DataFrame với 3 cột mà bạn sẽ nhận được
import pandas pd
#Lists containing test data
offices = ['Paris', 'Madrid', 'London', 'Barcelona', 'Brussels']
num_interviews = [129.0, 132.0, 145.0, 230.0, pd.NA]
positions = [12.0, 15.0, 13.0, 13.5, 3]
#Create DataFrame from dictionary
interviews_dict = dict[office=offices, total_interviews =num_interviews, total_positions = positions ]
interviews = pd.DataFrame[interviews_dict]
interviews.head[]
print[interviews.head[]]
26Sau đó, bạn có thể chỉ định nhiều cột [trong ví dụ này là hai cột đầu tiên] mà bạn muốn chuyển đổi thành số nguyên
import pandas pd
#Lists containing test data
offices = ['Paris', 'Madrid', 'London', 'Barcelona', 'Brussels']
num_interviews = [129.0, 132.0, 145.0, 230.0, pd.NA]
positions = [12.0, 15.0, 13.0, 13.5, 3]
#Create DataFrame from dictionary
interviews_dict = dict[office=offices, total_interviews =num_interviews, total_positions = positions ]
interviews = pd.DataFrame[interviews_dict]
interviews.head[]
print[interviews.head[]]
27Như bạn có thể quan sát, 2 cột đầu tiên hiện được chuyển thành số nguyên
import pandas pd
#Lists containing test data
offices = ['Paris', 'Madrid', 'London', 'Barcelona', 'Brussels']
num_interviews = [129.0, 132.0, 145.0, 230.0, pd.NA]
positions = [12.0, 15.0, 13.0, 13.5, 3]
#Create DataFrame from dictionary
interviews_dict = dict[office=offices, total_interviews =num_interviews, total_positions = positions ]
interviews = pd.DataFrame[interviews_dict]
interviews.head[]
print[interviews.head[]]
28[4] Chuyển đổi DataFrame chứa các giá trị NaN
Trong kịch bản cuối cùng, bạn sẽ thấy cách chuyển đổi một cột bao gồm hỗn hợp các giá trị float và NaN
Mục tiêu là chuyển đổi các giá trị float thành số nguyên, cũng như thay thế các giá trị NaN bằng số không
Đây là mã để tạo DataFrame
import pandas pd
#Lists containing test data
offices = ['Paris', 'Madrid', 'London', 'Barcelona', 'Brussels']
num_interviews = [129.0, 132.0, 145.0, 230.0, pd.NA]
positions = [12.0, 15.0, 13.0, 13.5, 3]
#Create DataFrame from dictionary
interviews_dict = dict[office=offices, total_interviews =num_interviews, total_positions = positions ]
interviews = pd.DataFrame[interviews_dict]
interviews.head[]
print[interviews.head[]]
29Bạn sẽ nhận được DataFrame này chứa cả số float và NaN
print [interviews.dtypes]
20Sau đó, bạn có thể thay thế các giá trị NaN bằng số 0 bằng cách thêm fillna[0], sau đó thực hiện chuyển đổi thành số nguyên bằng cách sử dụng astype[int]