Hướng dẫn python replace dot with comma - python thay thế dấu chấm bằng dấu phẩy

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.
  • df['Value'].str.replace['[A-Za-z]', ''].str.replace[',', '.'].astype[float]
    
    0    34500.00
    1    88782.21
    Name: Value, dtype: float64
    
    0 thay thế dấu phẩy cho một dấu chấm
  • df['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 float

Đã 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

  • Đọc
  • Bàn luận
  • 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
    
    3

    df['Value'].str.replace['[A-Za-z]', ''].str.replace[',', '.'].astype[float]
    
    0    34500.00
    1    88782.21
    Name: Value, dtype: float64
    
    4
    df['Value'].str.replace['[A-Za-z]', ''].str.replace[',', '.'].astype[float]
    
    0    34500.00
    1    88782.21
    Name: Value, dtype: float64
    
    5
    df['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
    
    7

    df['Value'].str.replace['[A-Za-z]', ''].str.replace[',', '.'].astype[float]
    
    0    34500.00
    1    88782.21
    Name: Value, dtype: float64
    
    4
    df['Value'].str.replace['[A-Za-z]', ''].str.replace[',', '.'].astype[float]
    
    0    34500.00
    1    88782.21
    Name: Value, dtype: float64
    
    9
    df['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
    
    7

    data['amount'] = data['amount'].apply[lambda x: x.replace['EUR', '']].apply[lambda x: x.replace['.', '']].apply[lambda x: x.replace[',', '.']].astype['float']
    
    8
    data['amount'] = data['amount'].apply[lambda x: x.replace['EUR', '']].apply[lambda x: x.replace['.', '']].apply[lambda x: x.replace[',', '.']].astype['float']
    
    9

    Output: 

    14.625.498, 002

    df['Value'].str.replace['[A-Za-z]', ''].str.replace[',', '.'].astype[float]
    
    0    34500.00
    1    88782.21
    Name: Value, dtype: float64
    
    4
             amount     amount2
    0   3.4500,00EUR    4000
    1   3.600,00EUR     500
    
    9
    data['amount'] = data['amount'].apply[lambda x: x.replace['EUR', '']].apply[lambda x: x.replace['.', '']].apply[lambda x: x.replace[',', '.']].astype['float']
    
    0
    data['amount'] = data['amount'].apply[lambda x: x.replace['EUR', '']].apply[lambda x: x.replace['.', '']].apply[lambda x: x.replace[',', '.']].astype['float']
    
    1
             amount     amount2
    0   3.4500,00EUR    4000
    1   3.600,00EUR     500
    
    3
    data['amount'] = data['amount'].apply[lambda x: x.replace['EUR', '']].apply[lambda x: x.replace['.', '']].apply[lambda x: x.replace[',', '.']].astype['float']
    
    3
    data['amount'] = data['amount'].apply[lambda x: x.replace['EUR', '']].apply[lambda x: x.replace['.', '']].apply[lambda x: x.replace[',', '.']].astype['float']
    
    4

    data['amount'] = data['amount'].apply[lambda x: x.replace['EUR', '']].apply[lambda x: x.replace['.', '']].apply[lambda x: x.replace[',', '.']].astype['float']
    
    5
    df['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']
    
    7
    Example:  

    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
    
    3

    df['Value'].str.replace['[A-Za-z]', ''].str.replace[',', '.'].astype[float]
    
    0    34500.00
    1    88782.21
    Name: Value, dtype: float64
    
    4
    df['Value'].str.replace['[A-Za-z]', ''].str.replace[',', '.'].astype[float]
    
    0    34500.00
    1    88782.21
    Name: Value, dtype: float64
    
    5
    df['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
    
    7

    df['Value'].str.replace['[A-Za-z]', ''].str.replace[',', '.'].astype[float]
    
    0    34500.00
    1    88782.21
    Name: Value, dtype: float64
    
    4
    df['Value'].str.replace['[A-Za-z]', ''].str.replace[',', '.'].astype[float]
    
    0    34500.00
    1    88782.21
    Name: Value, dtype: float64
    
    9
    df['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
    
    7

    df['Value'].str.replace['[A-Za-z]', ''].str.replace[',', '.'].astype[float]
    
    0    34500.00
    1    88782.21
    Name: Value, dtype: float64
    
    4
    df['Value'].str.replace['[A-Za-z]', ''].str.replace[',', '.'].astype[float]
    
    0    34500.00
    1    88782.21
    Name: Value, dtype: float64
    
    9
    df['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
    
    7

    data['amount'] = data['amount'].apply[lambda x: x.replace['EUR', '']].apply[lambda x: x.replace['.', '']].apply[lambda x: x.replace[',', '.']].astype['float']
    
    8
    data['amount'] = data['amount'].apply[lambda x: x.replace['EUR', '']].apply[lambda x: x.replace['.', '']].apply[lambda x: x.replace[',', '.']].astype['float']
    
    9

    Output: 

    14.625.498, 002

    Làm cách nào để thay đổi dấu phẩy thành dấu chấm trong Python?

    str.replace [',', '.'] thay thế dấu phẩy cho một dấu chấm. replace[',', '. '] replaces the comma for a dot.

    Làm thế nào để bạn thêm một dấu chấm vào một chuỗi trong Python?

    Làm thế nào để bạn chèn một dấu chấm trong Python? Viết một hàm có tên add_dots lấy một chuỗi và thêm "." ở giữa mỗi chữ cái. Ví dụ: gọi add_dots ["test"] sẽ trả về chuỗi "T.E.S.T". Sau đó, bên dưới hàm add_dots, viết một hàm khác có tên remove_dots loại bỏ tất cả các dấu chấm từ một chuỗi.Write a function named add_dots that takes a string and adds "." in between each letter. For example, calling add_dots["test"] should return the string "t.e.s.t" . Then, below the add_dots function, write another function named remove_dots that removes all dots from a string.

    Làm cách nào để thay đổi các giá trị trong một cột trong gấu trúc?

    Giả sử bạn muốn thay thế nhiều giá trị bằng nhiều giá trị mới cho cột DataFrame riêng lẻ.Trong trường hợp đó, bạn có thể sử dụng mẫu này: df ['tên cột'] = df ['tên cột'].Giá trị ',' Giá trị mới thứ 2 ', ...]]df['column name'] = df['column name']. replace[['1st old value', '2nd old value', ...], ['1st new value', '2nd new value', ...]]

    Làm thế nào để gấu trúc biến thành phao?

    Pandas chuyển đổi chuỗi thành float Sử dụng gấu trúc DataFrame.hàm asttype [] để chuyển đổi cột từ chuỗi/int sang float, bạn có thể áp dụng nó trên một cột cụ thể hoặc trên toàn bộ khung dữ liệu.Để chuyển kiểu dữ liệu lên float có chữ ký 54 bit, bạn có thể sử dụng Numpy.float64, numpy.Use pandas DataFrame. astype[] function to convert column from string/int to float, you can apply this on a specific column or on an entire DataFrame. To cast the data type to 54-bit signed float, you can use numpy. float64 , numpy.

    Bài Viết Liên Quan

    Chủ Đề