Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python &引用;10.0“;更换“时,将完全移除”;。0“;加上&引用;_Python_Pandas - Fatal编程技术网

Python &引用;10.0“;更换“时,将完全移除”;。0“;加上&引用;

Python &引用;10.0“;更换“时,将完全移除”;。0“;加上&引用;,python,pandas,Python,Pandas,我有一个包含浮点数字符串的dataframe列,我想在适用的情况下删除尾随的“.0”。但是,在执行df[“numbers”].str.replace(“.0”,“”“)时,字符串“10.0”将被完全删除,而不是替换为“10”。这似乎只会影响数字10、100等 MWE: 这是一个bug还是按预期工作?还请注意,使用这种方法将“10.1”更改为“.1”,这很奇怪。需要$来匹配字符串的结尾,并通过\转义: print (df.numbers.str.replace("\.0$", "")) 0

我有一个包含浮点数字符串的dataframe列,我想在适用的情况下删除尾随的“.0”。但是,在执行
df[“numbers”].str.replace(“.0”,“”“)
时,字符串“10.0”将被完全删除,而不是替换为“10”。这似乎只会影响数字10、100等

MWE:


这是一个bug还是按预期工作?还请注意,使用这种方法将“10.1”更改为“.1”,这很奇怪。

需要
$
来匹配
字符串的结尾,并通过
\
转义

print (df.numbers.str.replace("\.0$", ""))
0        1
1       10
2     10.1
3      100
4    100.1
5       99
Name: numbers, dtype: object

需要
$
以匹配
字符串的结尾,并通过
\
转义

print (df.numbers.str.replace("\.0$", ""))
0        1
1       10
2     10.1
3      100
4    100.1
5       99
Name: numbers, dtype: object

Dataframe.str.replace
采用正则表达式,因此
匹配任何字符。你想要

df.numbers.str.replace("\.0", "")

Dataframe.str.replace
采用正则表达式,因此
匹配任何字符。你想要

df.numbers.str.replace("\.0", "")