我有一個這樣的資料框
| 指數 | 識別符號 |
|---|---|
| 0 | 10769289.0 |
| 1 | 1082471174.0 |
“識別符號列是字串列”,我需要洗掉“.0”
我正在使用以下代碼:
Dataframe["Identifier"] = Dataframe["Identifier"].replace(regex=['.0'],value='')
但我得到了這個:
IndexIdentifier0769289182471174
如您所見,它不僅洗掉了“.0”。我也嘗試使用
Dataframe["Identifier"] = Dataframe["Identifier"].str.replace(".0", "")
但我得到了同樣的結果。
uj5u.com熱心網友回復:
正則運算式或替換中的點 ( .)可以表示字符。因此,您必須轉義小數點。否則它將替換任何后跟零的字符。在您的情況下,這意味著它將替換and開頭的 the ,以及每個數字末尾的 the 。通過轉義小數點,它只會查找以下內容: - 這是您想要的。any1010769289.01082471174.0.0.0
import pandas as pd
# Create the dataframe as per the example
Dataframe = pd.DataFrame({"Index": [0,1], "Identifier": ['10769289.0', '1082471174.0']})
# Replace the decimal and the zero at the end of each Identifier.
Dataframe["Identifier"] = Dataframe["Identifier"].str.replace("\.0", "")
# Print the dataframe
print(Dataframe)
輸出:
Index Identifier
0 0 10769289
1 1 1082471174
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/531130.html
上一篇:用malloc復制字串的正確方法
下一篇:自動附加的字串回圈
