Hướng dẫn how do i change the datatype of an object in python? - làm cách nào để thay đổi kiểu dữ liệu của một đối tượng trong python?
Ngày đăng:
12/11/2022
Trả lời:
0
Lượt xem:
110
Phần giới thiệu này về gấu trúc này có nguồn gốc từ câu hỏi đáp của Pandas của Trường Data với các ghi chú và mã của riêng tôi. Show
Thay đổi loại dữ liệu của loạt gấu trúcIn [2]: url = 'http://bit.ly/drinksbycountry' drinks = pd.read_csv(url) Out[3]:
Out[5]: country object beer_servings int64 spirit_servings int64 wine_servings int64 total_litres_of_pure_alcohol float64 continent object dtype: object Andorra
1 nổi (float64) In [8]: # to change use .astype() drinks['beer_servings'] = drinks.beer_servings.astype(float) Out[10]: country object beer_servings float64 spirit_servings int64 wine_servings int64 total_litres_of_pure_alcohol float64 continent object dtype: object 2 đối tượng (đối tượng) Phương pháp 1: Thay đổi kiểu dữ liệu sau khi đọc CSV drinks = pd.read_csv(url, dtype={'beer_servings':float}) Out[12]: country object beer_servings float64 spirit_servings int64 wine_servings int64 total_litres_of_pure_alcohol float64 continent object dtype: object In [13]: url = 'http://bit.ly/chiporders' orders = pd.read_table(url) Out[14]:
Out[15]: order_id int64 quantity int64 item_name object choice_description object item_price object dtype: object [Clementine] In [18]: # we use .str to replace and then convert to float orders['item_price'] = orders.item_price.str.replace('$', '').astype(float) Out[19]: order_id int64 quantity int64 item_name object choice_description object item_price float64 dtype: object In [20]: country object beer_servings int64 spirit_servings int64 wine_servings int64 total_litres_of_pure_alcohol float64 continent object dtype: object0 $ 3,39 In [22]: Nantucket Nectar Out[22]: country object beer_servings int64 spirit_servings int64 wine_servings int64 total_litres_of_pure_alcohol float64 continent object dtype: object2 In [23]: country object beer_servings int64 spirit_servings int64 wine_servings int64 total_litres_of_pure_alcohol float64 continent object dtype: object3 Out[23]: country object beer_servings int64 spirit_servings int64 wine_servings int64 total_litres_of_pure_alcohol float64 continent object dtype: object4 Làm thế nào để bạn thay đổi kiểu dữ liệu của một đối tượng trong Python?Thay đổi loại cột thành đối tượng chuỗi bằng cách sử dụng dataFrame.Asype () DataFrame.Phương thức ASTYPE () được sử dụng để đúc đối tượng gấu trúc cho một dtype được chỉ định.DataFrame. astype() DataFrame. astype() method is used to cast pandas object to a specified dtype.
Làm cách nào để thay đổi kiểu dữ liệu của một đối tượng trong gấu trúc?Để chuyển đổi các loại dữ liệu trong gấu trúc, có ba tùy chọn cơ bản: sử dụng astype () để buộc một dtype thích hợp.Tạo một chức năng tùy chỉnh để chuyển đổi dữ liệu.Sử dụng các hàm gấu trúc như to_numeric () hoặc to_dateTime ()Use astype() to force an appropriate dtype. Create a custom function to convert the data. Use pandas functions such as to_numeric() or to_datetime()
Làm thế nào để bạn thay đổi một loại dữ liệu từ một đối tượng sang nổi trong Python?Chúng ta có thể chuyển đổi một chuỗi thành float trong python bằng hàm float ().Đây là một chức năng tích hợp được sử dụng để chuyển đổi một đối tượng thành số điểm nổi.using the float() function. This is a built-in function used to convert an object to a floating point number.
Kiểu dữ liệu đối tượng trong Python là gì?Một đối tượng kiểu dữ liệu (một thể hiện của lớp DTYPE) mô tả cách các byte trong khối có kích thước cố định của bộ nhớ tương ứng với một mục mảng phải được giải thích.Nó mô tả các khía cạnh sau của dữ liệu: loại dữ liệu (số nguyên, float, đối tượng Python, v.v.)describes how the bytes in the fixed-size block of memory corresponding to an array item should be interpreted. It describes the following aspects of the data: Type of the data (integer, float, Python object, etc.) |