Thay đổi kiểu dữ liệu Python

countrybeer_servingsspirit_servingswine_servingstotal_lits_of_pure_alcoholcontinent0Afghanistan0000. 0Châu Á1Albania89132544. 9Châu Âu2Algeria250140. 7Châu Phi3Andorra24513831212. 4Châu Âu4Angola21757455. 9Châu Phi

Trong [5]

drinks.dtypes

Ra[5]

country                          object
beer_servings                     int64
spirit_servings                   int64
wine_servings                     int64
total_litres_of_pure_alcohol    float64
continent                        object
dtype: object

Tóm tắt kiểu dữ liệu

  • 3 số nguyên [int64]
  • 1 thả nổi [float64]
  • 2 đối tượng [đối tượng]

Phương pháp 1. Thay đổi kiểu dữ liệu sau khi đọc csv

Trong [8]

# to change use .astype[] 
drinks['beer_servings'] = drinks.beer_servings.astype[float]

Trong [10]

drinks.dtypes

Ra[10]

country                          object
beer_servings                   float64
spirit_servings                   int64
wine_servings                     int64
total_litres_of_pure_alcohol    float64
continent                        object
dtype: object

Phương pháp 2. Thay đổi kiểu dữ liệu trước khi đọc csv

Trong [11]

drinks = pd.read_csv[url, dtype={'beer_servings':float}]

Trong [12]

drinks.dtypes

Ra[12]

country                          object
beer_servings                   float64
spirit_servings                   int64
wine_servings                     int64
total_litres_of_pure_alcohol    float64
continent                        object
dtype: object

Trong [13]

url = '//bit.ly/drinksbycountry'
drinks = pd.read_csv[url]
1

Trong [14]

url = '//bit.ly/drinksbycountry'
drinks = pd.read_csv[url]
2

Ra[14]

order_idquantityitem_namechoice_descriptionitem_price011Chip chiên và Salsa cà chua tươiNaN$2. 39111Izze[Clementine]$3. 39211Nantucket Nectar[Apple]$3. 39311Khoai tây chiên và Salsa ớt xanh cà chuaNaN$2. 39422 Chicken Bowl[Tomatillo-Red Chili Salsa [Nóng], [Đậu đen. $16. 98

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 là True

Trả lại một bản sao khi

drinks.dtypes
3 [hãy rất cẩn thận khi đặt
country                          object
beer_servings                     int64
spirit_servings                   int64
wine_servings                     int64
total_litres_of_pure_alcohol    float64
continent                        object
dtype: object
00 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]

Khi làm việc trong Pandas DataFrame hoặc bất kỳ cấu trúc dữ liệu dạng bảng nào, chúng tôi thường được yêu cầu thay đổi kiểu dữ liệu [dtype] của một cột còn được gọi là truyền kiểu, ví dụ: chuyển đổi từ int thành chuỗi, chuỗi thành int e. t. c, Ở gấu trúc, bạn có thể làm điều này bằng cách sử dụng một số phương pháp như

country                          object
beer_servings                     int64
spirit_servings                   int64
wine_servings                     int64
total_litres_of_pure_alcohol    float64
continent                        object
dtype: object
15,
country                          object
beer_servings                     int64
spirit_servings                   int64
wine_servings                     int64
total_litres_of_pure_alcohol    float64
continent                        object
dtype: object
16,
country                          object
beer_servings                     int64
spirit_servings                   int64
wine_servings                     int64
total_litres_of_pure_alcohol    float64
continent                        object
dtype: object
17,
country                          object
beer_servings                     int64
spirit_servings                   int64
wine_servings                     int64
total_litres_of_pure_alcohol    float64
continent                        object
dtype: object
18 và e. t. c. Trong bài viết này, tôi sẽ giải thích các ví dụ khác nhau về cách thay đổi hoặc chuyển đổi loại dữ liệu trong Pandas DataFrame – chuyển đổi tất cả các cột thành một loại cụ thể, chuyển đổi một hoặc nhiều loại cột – chuyển đổi thành các loại số e. t. c

1. Ví dụ nhanh về thay đổi kiểu dữ liệu

Dưới đây là một số ví dụ nhanh về chuyển đổi kiểu dữ liệu cột trên Pandas DataFrame

country                          object
beer_servings                     int64
spirit_servings                   int64
wine_servings                     int64
total_litres_of_pure_alcohol    float64
continent                        object
dtype: object
5

Bây giờ hãy xem với một ví dụ. trước tiên, hãy tạo Khung dữ liệu Pandas với các tên cột ________ 119, ________ 170, ________ 171, ________ 172

country                          object
beer_servings                     int64
spirit_servings                   int64
wine_servings                     int64
total_litres_of_pure_alcohol    float64
continent                        object
dtype: object
1

Sản lượng dưới sản lượng

country                          object
beer_servings                     int64
spirit_servings                   int64
wine_servings                     int64
total_litres_of_pure_alcohol    float64
continent                        object
dtype: object
7

2. Khung dữ liệu. convert_dtypes[] để chuyển đổi kiểu dữ liệu trong Pandas

country                          object
beer_servings                     int64
spirit_servings                   int64
wine_servings                     int64
total_litres_of_pure_alcohol    float64
continent                        object
dtype: object
73 có sẵn trong Pandas DataFrame kể từ phiên bản 1. 0. 0, đây là phương pháp được sử dụng nhiều nhất vì nó tự động chuyển đổi các loại cột thành các loại tốt nhất có thể

Dưới đây là Cú pháp của

country                          object
beer_servings                     int64
spirit_servings                   int64
wine_servings                     int64
total_litres_of_pure_alcohol    float64
continent                        object
dtype: object
74

# to change use .astype[] 
drinks['beer_servings'] = drinks.beer_servings.astype[float]
4

Bây giờ, hãy xem một ví dụ đơn giản

# to change use .astype[] 
drinks['beer_servings'] = drinks.beer_servings.astype[float]
5

Sản lượng dưới sản lượng. Lưu ý rằng nó đã chuyển đổi các cột có loại

country                          object
beer_servings                     int64
spirit_servings                   int64
wine_servings                     int64
total_litres_of_pure_alcohol    float64
continent                        object
dtype: object
75 thành loại
country                          object
beer_servings                     int64
spirit_servings                   int64
wine_servings                     int64
total_litres_of_pure_alcohol    float64
continent                        object
dtype: object
76

# to change use .astype[] 
drinks['beer_servings'] = drinks.beer_servings.astype[float]
8

3. Khung dữ liệu. astype[] để thay đổi kiểu dữ liệu trong Pandas

Trong pandas DataFrame, hãy sử dụng

country                          object
beer_servings                     int64
spirit_servings                   int64
wine_servings                     int64
total_litres_of_pure_alcohol    float64
continent                        object
dtype: object
77 để chuyển đổi một loại thành một loại khác của một hoặc nhiều cột cùng một lúc, bạn cũng có thể sử dụng nó để thay đổi tất cả các loại cột thành cùng một loại. Khi bạn thực hiện astype[] trên DataFrame mà không chỉ định tên cột, nó sẽ thay đổi tất cả các cột thành một loại cụ thể. Để chuyển đổi một cột cụ thể, bạn cần chỉ định rõ ràng cột đó

Dưới đây là cú pháp

country                          object
beer_servings                     int64
spirit_servings                   int64
wine_servings                     int64
total_litres_of_pure_alcohol    float64
continent                        object
dtype: object
78

drinks.dtypes
1

3. 1 Thay đổi tất cả các cột thành cùng loại trong Pandas

country                          object
beer_servings                     int64
spirit_servings                   int64
wine_servings                     int64
total_litres_of_pure_alcohol    float64
continent                        object
dtype: object
79 chuyển đổi tất cả các cột của Pandas DataFrame thành loại
country                          object
beer_servings                     int64
spirit_servings                   int64
wine_servings                     int64
total_litres_of_pure_alcohol    float64
continent                        object
dtype: object
76

drinks.dtypes
4

Sản lượng dưới sản lượng

drinks.dtypes
5

3. 2 Thay đổi loại cho một hoặc nhiều cột trong Pandas

Trên

country                          object
beer_servings                     int64
spirit_servings                   int64
wine_servings                     int64
total_litres_of_pure_alcohol    float64
continent                        object
dtype: object
15 Chỉ định tham số dưới dạng ký hiệu JSON với tên cột là khóa và loại bạn muốn chuyển đổi thành giá trị để thay đổi một hoặc nhiều cột. Ví dụ bên dưới truyền cột DataFrame loại
country                          object
beer_servings                     int64
spirit_servings                   int64
wine_servings                     int64
total_litres_of_pure_alcohol    float64
continent                        object
dtype: object
70 thành loại
# to change use .astype[] 
drinks['beer_servings'] = drinks.beer_servings.astype[float]
43 và loại
country                          object
beer_servings                     int64
spirit_servings                   int64
wine_servings                     int64
total_litres_of_pure_alcohol    float64
continent                        object
dtype: object
72 thành loại
# to change use .astype[] 
drinks['beer_servings'] = drinks.beer_servings.astype[float]
45

country                          object
beer_servings                   float64
spirit_servings                   int64
wine_servings                     int64
total_litres_of_pure_alcohol    float64
continent                        object
dtype: object
1

3. 3 Chuyển đổi kiểu dữ liệu cho tất cả các cột trong danh sách

Đôi khi, bạn có thể cần chuyển đổi danh sách các cột DataFrame thành một loại cụ thể, bạn có thể đạt được điều này theo một số cách. Dưới đây là 3 cách khác nhau để chuyển đổi cột _________ 170 và ________ 172 thành loại ________ 245

country                          object
beer_servings                     int64
spirit_servings                   int64
wine_servings                     int64
total_litres_of_pure_alcohol    float64
continent                        object
dtype: object
10

3. 4 Tăng hoặc bỏ qua lỗi khi chuyển đổi loại cột không thành công

Theo mặc định, khi bạn đang cố gắng thay đổi một cột thành loại không được hỗ trợ với dữ liệu, Pandas sẽ tạo ra một lỗi, để bỏ qua lỗi sử dụng thông số lỗi; . Trong ví dụ dưới đây, tôi đang chuyển đổi một cột có giá trị chuỗi thành int, cột này không được hỗ trợ nên nó phát sinh lỗi, tôi đã sử dụng

# to change use .astype[] 
drinks['beer_servings'] = drinks.beer_servings.astype[float]
49 để bỏ qua lỗi

country                          object
beer_servings                     int64
spirit_servings                   int64
wine_servings                     int64
total_litres_of_pure_alcohol    float64
continent                        object
dtype: object
11

4. Khung dữ liệu. infer_objects[] để thay đổi kiểu dữ liệu trong Pandas

Sử dụng phương pháp

# to change use .astype[] 
drinks['beer_servings'] = drinks.beer_servings.astype[float]
50 để tự động chuyển đổi các cột đối tượng thành một loại dữ liệu mà nó đang nắm giữ. Nó kiểm tra dữ liệu của từng cột đối tượng và tự động chuyển thành kiểu dữ liệu. Lưu ý rằng nó chỉ chuyển đổi các loại đối tượng. Ví dụ: nếu một cột có loại đối tượng đang giữ các loại int hoặc float, sử dụng infer_object[] sẽ chuyển đổi nó thành các loại tương ứng

country                          object
beer_servings                     int64
spirit_servings                   int64
wine_servings                     int64
total_litres_of_pure_alcohol    float64
continent                        object
dtype: object
12

5. Sử dụng Khung dữ liệu. to_numeric[] để chuyển đổi kiểu số

# to change use .astype[] 
drinks['beer_servings'] = drinks.beer_servings.astype[float]
51 được dùng để chuyển đổi các cột không phải là số 
# to change use .astype[] 
drinks['beer_servings'] = drinks.beer_servings.astype[float]
52 sang loại số phù hợp nhất

5. 1 Chuyển đổi kiểu số

Ví dụ dưới đây chỉ chuyển đổi cột

country                          object
beer_servings                     int64
spirit_servings                   int64
wine_servings                     int64
total_litres_of_pure_alcohol    float64
continent                        object
dtype: object
70 thành kiểu số

country                          object
beer_servings                     int64
spirit_servings                   int64
wine_servings                     int64
total_litres_of_pure_alcohol    float64
continent                        object
dtype: object
13

5. 2 Chuyển đổi nhiều kiểu số bằng phương thức apply[]

Sử dụng phương pháp

country                          object
beer_servings                     int64
spirit_servings                   int64
wine_servings                     int64
total_litres_of_pure_alcohol    float64
continent                        object
dtype: object
16 cùng với phương thức
# to change use .astype[] 
drinks['beer_servings'] = drinks.beer_servings.astype[float]
55 để chuyển đổi nhiều cột thành kiểu số. Ví dụ dưới đây chuyển đổi cột
country                          object
beer_servings                     int64
spirit_servings                   int64
wine_servings                     int64
total_litres_of_pure_alcohol    float64
continent                        object
dtype: object
70 và
country                          object
beer_servings                     int64
spirit_servings                   int64
wine_servings                     int64
total_litres_of_pure_alcohol    float64
continent                        object
dtype: object
72 thành kiểu số

country                          object
beer_servings                     int64
spirit_servings                   int64
wine_servings                     int64
total_litres_of_pure_alcohol    float64
continent                        object
dtype: object
14

Phần kết luận

Trong bài viết này, bạn đã học cách chuyển đổi/thay đổi tất cả các cột của DataFrame thành một loại cụ thể, trường hợp một hoặc nhiều cột và cuối cùng là chuyển đổi các cột thành loại số bằng cách sử dụng các phương pháp

country                          object
beer_servings                     int64
spirit_servings                   int64
wine_servings                     int64
total_litres_of_pure_alcohol    float64
continent                        object
dtype: object
15,
country                          object
beer_servings                     int64
spirit_servings                   int64
wine_servings                     int64
total_litres_of_pure_alcohol    float64
continent                        object
dtype: object
16,
country                          object
beer_servings                     int64
spirit_servings                   int64
wine_servings                     int64
total_litres_of_pure_alcohol    float64
continent                        object
dtype: object
17,
country                          object
beer_servings                     int64
spirit_servings                   int64
wine_servings                     int64
total_litres_of_pure_alcohol    float64
continent                        object
dtype: object
18

Chủ Đề