Tôi có một cột có dữ liệu như 3,4500,00 EUR. Bây giờ tôi muốn so sánh điều này với một cột khác có số float như 4000.00. Làm cách nào để lấy chuỗi này, loại bỏ EUR và thay thế dấu phẩy bằng thập phân và sau đó chuyển đổi thành phao để so sánh?
bad_coder
9.81620 Huy hiệu vàng39 Huy hiệu bạc64 Huy hiệu đồng20 gold badges39 silver badges64 bronze badges
Đã hỏi ngày 26 tháng 5 năm 2019 lúc 16:39May 26, 2019 at 16:39
2
Bạn có thể sử dụng các biểu thức thông thường để làm cho các điều kiện của bạn chung hoạt động trong mọi trường hợp:
# Make example dataframe for showing answer
df = pd.DataFrame[{'Value':['3.4500,00 EUR', '88.782,21 DOLLAR']}]
Value
0 3.4500,00 EUR
1 88.782,21 DOLLAR
Sử dụng str.replace
với biểu thức thông thường:
df['Value'].str.replace['[A-Za-z]', ''].str.replace[',', '.'].astype[float]
0 34500.00
1 88782.21
Name: Value, dtype: float64
Explanation:
str.replace['[A-Za-z\.]', '']
loại bỏ tất cả các ký tự chữ cái và dấu chấm.
0 thay thế dấu phẩy cho một dấu chấmdf['Value'].str.replace['[A-Za-z]', ''].str.replace[',', '.'].astype[float] 0 34500.00 1 88782.21 Name: Value, dtype: float64
1 chuyển đổi nó từ loại đối tượng [chuỗi] thành floatdf['Value'].str.replace['[A-Za-z]', ''].str.replace[',', '.'].astype[float] 0 34500.00 1 88782.21 Name: Value, dtype: float64
Đã trả lời ngày 26 tháng 5 năm 2019 lúc 17:02May 26, 2019 at 17:02
0
Đây là giải pháp của tôi:
Dữ liệu giả:
amount amount2
0 3.4500,00EUR 4000
1 3.600,00EUR 500
sử dụng Ứng dụng [] sau đó chuyển đổi kiểu dữ liệu thành float
data['amount'] = data['amount'].apply[lambda x: x.replace['EUR', '']].apply[lambda x: x.replace['.', '']].apply[lambda x: x.replace[',', '.']].astype['float']
result:
amount amount2
0 34500.0 4000
1 3600.0 500
Đã trả lời ngày 26 tháng 5 năm 2019 lúc 17:01May 26, 2019 at 17:01
Xp.LXp.LXp.L
7671 Huy hiệu vàng6 Huy hiệu bạc12 Huy hiệu đồng1 gold badge6 silver badges12 bronze badges
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Examples:
Input : 14, 625, 498.002 Output : 14.625.498, 002
Bàn luận
Vấn đề khá đơn giản. Đưa ra một chuỗi, chúng ta cần thay thế tất cả các dấu phẩy bằng các dấu chấm và tất cả các dấu chấm bằng dấu phẩy. Điều này có thể đạt được theo hai cách phổ biến. & Nbsp; ví dụ: & nbsp; This static method returns a translation table usable for str.translate[]. This builds a translation table, which is a mapping of integers or characters to integers, strings, or None.
translate: This returns a copy of the string where all characters occurring in the optional argument are removed, and the remaining characters have been
mapped through the translation table, given by the maketrans table.
For more reference visit Python String Methods.
Python3
Sử dụng maketrans và dịch []
Maketrans: Phương thức tĩnh này trả về một bảng dịch có thể sử dụng cho str.translate []. Điều này xây dựng một bảng dịch, là một ánh xạ các số nguyên hoặc ký tự cho số nguyên, chuỗi hoặc không có. bảng dịch, được đưa ra bởi bảng Maketrans. & nbsp; để biết thêm tham chiếu truy cập các phương thức chuỗi python. & nbsp; & nbsp;
df['Value'].str.replace['[A-Za-z]', ''].str.replace[',', '.'].astype[float]
0 34500.00
1 88782.21
Name: Value, dtype: float64
2 df['Value'].str.replace['[A-Za-z]', ''].str.replace[',', '.'].astype[float]
0 34500.00
1 88782.21
Name: Value, dtype: float64
3df['Value'].str.replace['[A-Za-z]', ''].str.replace[',', '.'].astype[float]
0 34500.00
1 88782.21
Name: Value, dtype: float64
4df['Value'].str.replace['[A-Za-z]', ''].str.replace[',', '.'].astype[float]
0 34500.00
1 88782.21
Name: Value, dtype: float64
5df['Value'].str.replace['[A-Za-z]', ''].str.replace[',', '.'].astype[float]
0 34500.00
1 88782.21
Name: Value, dtype: float64
6 df['Value'].str.replace['[A-Za-z]', ''].str.replace[',', '.'].astype[float]
0 34500.00
1 88782.21
Name: Value, dtype: float64
7df['Value'].str.replace['[A-Za-z]', ''].str.replace[',', '.'].astype[float]
0 34500.00
1 88782.21
Name: Value, dtype: float64
4df['Value'].str.replace['[A-Za-z]', ''].str.replace[',', '.'].astype[float]
0 34500.00
1 88782.21
Name: Value, dtype: float64
9df['Value'].str.replace['[A-Za-z]', ''].str.replace[',', '.'].astype[float]
0 34500.00
1 88782.21
Name: Value, dtype: float64
6 amount amount2
0 3.4500,00EUR 4000
1 3.600,00EUR 500
1 amount amount2
0 3.4500,00EUR 4000
1 3.600,00EUR 500
2223 amount amount2
0 3.4500,00EUR 4000
1 3.600,00EUR 500
4 amount amount2
0 3.4500,00EUR 4000
1 3.600,00EUR 500
3 amount amount2
0 3.4500,00EUR 4000
1 3.600,00EUR 500
6 amount amount2
0 3.4500,00EUR 4000
1 3.600,00EUR 500
7data['amount'] = data['amount'].apply[lambda x: x.replace['EUR', '']].apply[lambda x: x.replace['.', '']].apply[lambda x: x.replace[',', '.']].astype['float']
8data['amount'] = data['amount'].apply[lambda x: x.replace['EUR', '']].apply[lambda x: x.replace['.', '']].apply[lambda x: x.replace[',', '.']].astype['float']
9Output:
14.625.498, 002
4df['Value'].str.replace['[A-Za-z]', ''].str.replace[',', '.'].astype[float]
0 34500.00
1 88782.21
Name: Value, dtype: float64
9 amount amount2
0 3.4500,00EUR 4000
1 3.600,00EUR 500
0data['amount'] = data['amount'].apply[lambda x: x.replace['EUR', '']].apply[lambda x: x.replace['.', '']].apply[lambda x: x.replace[',', '.']].astype['float']
1data['amount'] = data['amount'].apply[lambda x: x.replace['EUR', '']].apply[lambda x: x.replace['.', '']].apply[lambda x: x.replace[',', '.']].astype['float']
3 amount amount2
0 3.4500,00EUR 4000
1 3.600,00EUR 500
3data['amount'] = data['amount'].apply[lambda x: x.replace['EUR', '']].apply[lambda x: x.replace['.', '']].apply[lambda x: x.replace[',', '.']].astype['float']
4data['amount'] = data['amount'].apply[lambda x: x.replace['EUR', '']].apply[lambda x: x.replace['.', '']].apply[lambda x: x.replace[',', '.']].astype['float']
data['amount'] = data['amount'].apply[lambda x: x.replace['EUR', '']].apply[lambda x: x.replace['.', '']].apply[lambda x: x.replace[',', '.']].astype['float']
5df['Value'].str.replace['[A-Za-z]', ''].str.replace[',', '.'].astype[float]
0 34500.00
1 88782.21
Name: Value, dtype: float64
6 data['amount'] = data['amount'].apply[lambda x: x.replace['EUR', '']].apply[lambda x: x.replace['.', '']].apply[lambda x: x.replace[',', '.']].astype['float']
7Example:
Python3
Sử dụng maketrans và dịch []
Maketrans: Phương thức tĩnh này trả về một bảng dịch có thể sử dụng cho str.translate []. Điều này xây dựng một bảng dịch, là một ánh xạ các số nguyên hoặc ký tự cho số nguyên, chuỗi hoặc không có. bảng dịch, được đưa ra bởi bảng Maketrans. & nbsp; để biết thêm tham chiếu truy cập các phương thức chuỗi python. & nbsp; & nbsp;
df['Value'].str.replace['[A-Za-z]', ''].str.replace[',', '.'].astype[float]
0 34500.00
1 88782.21
Name: Value, dtype: float64
2 df['Value'].str.replace['[A-Za-z]', ''].str.replace[',', '.'].astype[float]
0 34500.00
1 88782.21
Name: Value, dtype: float64
3df['Value'].str.replace['[A-Za-z]', ''].str.replace[',', '.'].astype[float]
0 34500.00
1 88782.21
Name: Value, dtype: float64
4df['Value'].str.replace['[A-Za-z]', ''].str.replace[',', '.'].astype[float]
0 34500.00
1 88782.21
Name: Value, dtype: float64
5df['Value'].str.replace['[A-Za-z]', ''].str.replace[',', '.'].astype[float]
0 34500.00
1 88782.21
Name: Value, dtype: float64
6 df['Value'].str.replace['[A-Za-z]', ''].str.replace[',', '.'].astype[float]
0 34500.00
1 88782.21
Name: Value, dtype: float64
7df['Value'].str.replace['[A-Za-z]', ''].str.replace[',', '.'].astype[float]
0 34500.00
1 88782.21
Name: Value, dtype: float64
4df['Value'].str.replace['[A-Za-z]', ''].str.replace[',', '.'].astype[float]
0 34500.00
1 88782.21
Name: Value, dtype: float64
9df['Value'].str.replace['[A-Za-z]', ''].str.replace[',', '.'].astype[float]
0 34500.00
1 88782.21
Name: Value, dtype: float64
6 amount amount2
0 3.4500,00EUR 4000
1 3.600,00EUR 500
1 amount amount2
0 3.4500,00EUR 4000
1 3.600,00EUR 500
2223 amount amount2
0 3.4500,00EUR 4000
1 3.600,00EUR 500
4 amount amount2
0 3.4500,00EUR 4000
1 3.600,00EUR 500
3 amount amount2
0 3.4500,00EUR 4000
1 3.600,00EUR 500
6 amount amount2
0 3.4500,00EUR 4000
1 3.600,00EUR 500
7df['Value'].str.replace['[A-Za-z]', ''].str.replace[',', '.'].astype[float]
0 34500.00
1 88782.21
Name: Value, dtype: float64
4df['Value'].str.replace['[A-Za-z]', ''].str.replace[',', '.'].astype[float]
0 34500.00
1 88782.21
Name: Value, dtype: float64
9df['Value'].str.replace['[A-Za-z]', ''].str.replace[',', '.'].astype[float]
0 34500.00
1 88782.21
Name: Value, dtype: float64
6 amount amount2
0 3.4500,00EUR 4000
1 3.600,00EUR 500
1 amount amount2
0 3.4500,00EUR 4000
1 3.600,00EUR 500
2223 amount amount2
0 3.4500,00EUR 4000
1 3.600,00EUR 500
4 amount amount2
0 3.4500,00EUR 4000
1 3.600,00EUR 500
3 amount amount2
0 3.4500,00EUR 4000
1 3.600,00EUR 500
6 amount amount2
0 3.4500,00EUR 4000
1 3.600,00EUR 500
7data['amount'] = data['amount'].apply[lambda x: x.replace['EUR', '']].apply[lambda x: x.replace['.', '']].apply[lambda x: x.replace[',', '.']].astype['float']
8data['amount'] = data['amount'].apply[lambda x: x.replace['EUR', '']].apply[lambda x: x.replace['.', '']].apply[lambda x: x.replace[',', '.']].astype['float']
9Output:
14.625.498, 002