仅将“,”替换为“.”在一个字符串中 Python
Replace only "," by "." in a String Python
我正在使用包含列“Streams”dtype: object 的数据集,我只需要将“,”替换为“.”稍后使用 pandas.to_numeric() 并通过 float64 转换 String。有没有办法只更换
字符并保留数字?
示例:48,633,449 到 48.633.449
代码:
import pandas as pd
import numpy as np
dados = pd.read_csv("spotify_dataset.csv")
dados.dropna()
dados['Streams'].replace(",", ".")
dados['Streams'] = pd.to_numeric(dados['Streams'])
dados.head()
得到这个:
ValueError: Unable to parse string "48,633,449" at position 0
[错误]
import pandas as pd
import numpy as np
dados = pd.read_csv("spotify_dataset.csv")
dados = dados.dropna()
dados['Streams'] = dados['Streams'].replace(",", ".")
dados['Streams'] = pd.to_numeric(dados['Streams'])
dados.head()
您正在丢弃您的 replace
,因为您没有将它分配给任何东西。除非您显式使用 inplace=True
参数,否则 Pandas 方法不会更改对象(系列、数据帧)的当前实例。
您可以提供 replace
的结果作为 to_numeric
函数的参数
import pandas as pd
import numpy as np
dados = pd.read_csv("spotify_dataset.csv")
dados = dados.dropna()
dados['Streams'] = pd.to_numeric(dados['Streams'].replace(",", "."))
dados.head()
您应该使用 .str.replace
而不是 .replace
。
dados['Streams'] = pd.to_numeric(dados['Streams'].str.replace(",", ""))
另外,我不认为你的意图是用小数代替逗号。这将导致相同的错误,因为多个小数无效。
我正在使用包含列“Streams”dtype: object 的数据集,我只需要将“,”替换为“.”稍后使用 pandas.to_numeric() 并通过 float64 转换 String。有没有办法只更换 字符并保留数字?
示例:48,633,449 到 48.633.449
代码:
import pandas as pd
import numpy as np
dados = pd.read_csv("spotify_dataset.csv")
dados.dropna()
dados['Streams'].replace(",", ".")
dados['Streams'] = pd.to_numeric(dados['Streams'])
dados.head()
得到这个:
ValueError: Unable to parse string "48,633,449" at position 0
[错误]
import pandas as pd
import numpy as np
dados = pd.read_csv("spotify_dataset.csv")
dados = dados.dropna()
dados['Streams'] = dados['Streams'].replace(",", ".")
dados['Streams'] = pd.to_numeric(dados['Streams'])
dados.head()
您正在丢弃您的 replace
,因为您没有将它分配给任何东西。除非您显式使用 inplace=True
参数,否则 Pandas 方法不会更改对象(系列、数据帧)的当前实例。
您可以提供 replace
的结果作为 to_numeric
函数的参数
import pandas as pd
import numpy as np
dados = pd.read_csv("spotify_dataset.csv")
dados = dados.dropna()
dados['Streams'] = pd.to_numeric(dados['Streams'].replace(",", "."))
dados.head()
您应该使用 .str.replace
而不是 .replace
。
dados['Streams'] = pd.to_numeric(dados['Streams'].str.replace(",", ""))
另外,我不认为你的意图是用小数代替逗号。这将导致相同的错误,因为多个小数无效。