在数据框中替换'%',然后将所有字符串转换为浮点数



我在 *.csv 中有带有以下标头的数据:

locationA_hhs locationA_hhs_ratio locationB_hhs locationB_hhs_ratio locationC_hhs locationC_hhs_ratio locationD_hhs locationD_hhs_ratio

下面是此文件的一行内容:

16 0.52% 19 0.88% 14 0.46% 17 0.29%

我只需要将百分比读入为浮点数。 假设我已经将 *.csv 读入数据帧,我尝试仅选择以"ratio"结尾的列名,将"%"替换为",然后将所有这些列转换为浮点类型。但是此代码不会产生该结果。 请帮忙!

df_raw.select(lambda col: col.endswith('ratio'), axis=1).replace('%','').astype(float)

replace(X)替换完整的值X,而不是值的一部分。您必须使用regex=True选项:

result = df_raw.loc[:, df.columns.str.endswith('_ratio')]
.replace('%', '', regex=True).astype(float)

您可以使用双百分号%%来防止它被解释,以便您可以打印实际的%符号:

如下例:

test = "have it break."
selectiveEscape = "Print percent %% in sentence and not %s" % test
print selectiveEscape
Print percent % in sentence and not have it break.

最新更新