Bây giờ, hãy tạo một DataFrame với một vài hàng và cột, thực hiện các ví dụ trên và xác thực kết quả. Khung dữ liệu của chúng tôi chứa các tên cột
import pandas as pd
import numpy as np
technologies= [{
'Fee' :['22000.30','25000.40','23000.20','24000.50','26000.10'],
'Discount':['1000.10',np.nan,'1000.5',np.nan,'2500.20']
}]
df = pd.DataFrame[technologies]
print[df.dtypes]
8 và
import pandas as pd
import numpy as np
technologies= [{
'Fee' :['22000.30','25000.40','23000.20','24000.50','26000.10'],
'Discount':['1000.10',np.nan,'1000.5',np.nan,'2500.20']
}]
df = pd.DataFrame[technologies]
print[df.dtypes]
9
import pandas as pd
import numpy as np
technologies= [{
'Fee' :['22000.30','25000.40','23000.20','24000.50','26000.10'],
'Discount':['1000.10',np.nan,'1000.5',np.nan,'2500.20']
}]
df = pd.DataFrame[technologies]
print[df.dtypes]
Bạn có thể xác định loại dữ liệu của từng cột bằng cách sử dụng ______3_______0. Ví dụ, kết quả đầu ra của
Fee object
Discount object
dtype: object
1 như bên dưới. Đối tượng ở đây có nghĩa là một loại String.
Fee object
Discount object
dtype: object
2. gấu trúc Chuyển đổi chuỗi thành nổi
Sử dụng pandas DataFrame. astype[] để chuyển đổi cột từ chuỗi/int thành float, bạn có thể áp dụng điều này trên một cột cụ thể hoặc trên toàn bộ DataFrame. Để truyền loại dữ liệu sang phao có dấu 54 bit, bạn có thể sử dụng
Fee object
Discount object
dtype: object
2,
Fee object
Discount object
dtype: object
3,
Fee object
Discount object
dtype: object
4,
Fee object
Discount object
dtype: object
5 làm tham số. Để chuyển sang số float có dấu 32 bit, hãy sử dụng
Fee object
Discount object
dtype: object
6 hoặc
Fee object
Discount object
dtype: object
7Ví dụ bên dưới chuyển đổi cột
import pandas as pd
import numpy as np
technologies= [{
'Fee' :['22000.30','25000.40','23000.20','24000.50','26000.10'],
'Discount':['1000.10',np.nan,'1000.5',np.nan,'2500.20']
}]
df = pd.DataFrame[technologies]
print[df.dtypes]
8 từ chuỗi dtype thành
Fee object
Discount object
dtype: object
5.
# Convert "Fee" from string to float
df = df.astype[{'Fee':'float'}]
print[df.dtypes]
Sản lượng dưới sản lượng
Fee float64
Discount object
dtype: object
Bạn cũng có thể sử dụng Sê-ri. astype[] để chuyển đổi một cột cụ thể. vì mỗi cột trên DataFrame là Pandas Series, nên tôi sẽ lấy cột từ DataFrame dưới dạng Series và sử dụng hàm _______14_______0. Trong ví dụ bên dưới
# Convert "Fee" from string to float
df = df.astype[{'Fee':'float'}]
print[df.dtypes]
1 hoặc
# Convert "Fee" from string to float
df = df.astype[{'Fee':'float'}]
print[df.dtypes]
2 trả về đối tượng Sê-ri
# Convert "Fee" from string to float
df['Fee'] = df['Fee'].astype[float]
print[df.dtypes]
Mang lại sản lượng tương tự như trên
3. Chuyển đổi nhiều cột thành nổi
Bạn cũng có thể chuyển đổi nhiều cột thành float bằng cách gửi dict của tên cột -> loại dữ liệu đến phương thức
# Convert "Fee" from string to float
df = df.astype[{'Fee':'float'}]
print[df.dtypes]
0. Ví dụ dưới đây chuyển đổi cột
import pandas as pd
import numpy as np
technologies= [{
'Fee' :['22000.30','25000.40','23000.20','24000.50','26000.10'],
'Discount':['1000.10',np.nan,'1000.5',np.nan,'2500.20']
}]
df = pd.DataFrame[technologies]
print[df.dtypes]
8 và
import pandas as pd
import numpy as np
technologies= [{
'Fee' :['22000.30','25000.40','23000.20','24000.50','26000.10'],
'Discount':['1000.10',np.nan,'1000.5',np.nan,'2500.20']
}]
df = pd.DataFrame[technologies]
print[df.dtypes]
9 thành float dtype
# Convert multiple columns
df = df.astype[{'Fee':'float','Discount':'float'}]
4. Chuyển đổi tất cả các cột thành kiểu nổi
Theo mặc định, hàm astype[] chuyển đổi tất cả các cột thành cùng loại. Ví dụ dưới đây chuyển đổi tất cả các cột DataFrame thành kiểu float. Nếu bạn có bất kỳ cột nào có giá trị chữ và số, bạn sẽ gặp lỗi
# Convert entire DataFrame string to float
df = df.astype[float]
print[df.dtypes]
Sản lượng dưới sản lượng
Fee float64
Discount float64
dtype: object
5. Sử dụng gấu trúc. to_numeric[]
Ngoài ra, bạn có thể chuyển đổi tất cả các cột chuỗi thành kiểu float bằng cách sử dụng
import pandas as pd
import numpy as np
technologies= [{
'Fee' :['22000.30','25000.40','23000.20','24000.50','26000.10'],
'Discount':['1000.10',np.nan,'1000.5',np.nan,'2500.20']
}]
df = pd.DataFrame[technologies]
print[df.dtypes]
7. Ví dụ: sử dụng hàm
# Convert "Fee" from string to float
df = df.astype[{'Fee':'float'}]
print[df.dtypes]
7 để chuyển đổi cột
# Convert "Fee" from string to float
df = df.astype[{'Fee':'float'}]
print[df.dtypes]
8 thành float
# Convert numeric function string to float
df['Discount'] = pd.to_numeric[df['Discount']]
print[df.dtypes]
# Convert DataFrame column from string to float
df["Discount"] = pd.to_numeric[df["Discount"], downcast="float"]
print[df.dtypes]
Sản lượng dưới sản lượng
import pandas as pd
import numpy as np
technologies= [{
'Fee' :['22000.30','25000.40','23000.20','24000.50','26000.10'],
'Discount':['1000.10',np.nan,'1000.5',np.nan,'2500.20']
}]
df = pd.DataFrame[technologies]
print[df.dtypes]
06. Xử lý các giá trị không phải là số
Khi bạn có một số ô có giá trị ký tự trên một cột mà bạn muốn chuyển thành float, nó sẽ trả về lỗi. Để bỏ qua lỗi và chuyển đổi các giá trị ký tự thành
# Convert "Fee" from string to float
df = df.astype[{'Fee':'float'}]
print[df.dtypes]
9, hãy sử dụng thuộc tính
Fee float64
Discount object
dtype: object
0
import pandas as pd
import numpy as np
technologies= [{
'Fee' :['22000.30','25000.40','23000.20','24000.50','26000.10'],
'Discount':['1000.10',np.nan,'1000.5',np.nan,'2500.20']
}]
df = pd.DataFrame[technologies]
print[df.dtypes]
1Điều này mang lại đầu ra tương tự như trên
7. Thay thế các giá trị 'NaN' bằng Zeros
Sử dụng hàm
Fee float64
Discount object
dtype: object
1 để thay thế các giá trị
Fee float64
Discount object
dtype: object
2 bằng các giá trị '0'
import pandas as pd
import numpy as np
technologies= [{
'Fee' :['22000.30','25000.40','23000.20','24000.50','26000.10'],
'Discount':['1000.10',np.nan,'1000.5',np.nan,'2500.20']
}]
df = pd.DataFrame[technologies]
print[df.dtypes]
2Sản lượng dưới sản lượng
import pandas as pd
import numpy as np
technologies= [{
'Fee' :['22000.30','25000.40','23000.20','24000.50','26000.10'],
'Discount':['1000.10',np.nan,'1000.5',np.nan,'2500.20']
}]
df = pd.DataFrame[technologies]
print[df.dtypes]
38. Thay thế chuỗi rỗng trước khi chuyển đổi
Nếu bạn có các giá trị trống trong một chuỗi, hãy chuyển đổi chuỗi rỗng
Fee float64
Discount object
dtype: object
3 bằng np. nan trước khi chuyển nó thành float
import pandas as pd
import numpy as np
technologies= [{
'Fee' :['22000.30','25000.40','23000.20','24000.50','26000.10'],
'Discount':['1000.10',np.nan,'1000.5',np.nan,'2500.20']
}]
df = pd.DataFrame[technologies]
print[df.dtypes]
4Sản lượng dưới sản lượng
import pandas as pd
import numpy as np
technologies= [{
'Fee' :['22000.30','25000.40','23000.20','24000.50','26000.10'],
'Discount':['1000.10',np.nan,'1000.5',np.nan,'2500.20']
}]
df = pd.DataFrame[technologies]
print[df.dtypes]
5Phần kết luận
Trong bài viết này, bạn đã học cách chuyển đổi một cột, nhiều cột và tất cả các cột từ kiểu chuỗi sang kiểu float trong Pandas DataFrame bằng cách sử dụng hàm
Fee float64
Discount object
dtype: object
4 và
import pandas as pd
import numpy as np
technologies= [{
'Fee' :['22000.30','25000.40','23000.20','24000.50','26000.10'],
'Discount':['1000.10',np.nan,'1000.5',np.nan,'2500.20']
}]
df = pd.DataFrame[technologies]
print[df.dtypes]
7