简体   繁体   English

替换 Pandas DataFrame 中的字符

[英]replace characters in Pandas DataFrame

I want to export following chart to LaTex and replace my '+/-' with '±', just for the looks.我想将以下图表导出到 LaTex 并将我的 '+/-' 替换为 '±',只是为了外观。 Though, no matter what character I try (could also be an 'a' or anything else) it either doesn't change a bit or the column is empty.但是,无论我尝试什么字符(也可以是“a”或其他任何字符),它要么不会改变一点,要么该列是空的。 I found the code on several forums and I don't know what I'm doing wrong.我在几个论坛上找到了代码,我不知道我做错了什么。

df = pd.DataFrame({'A': ["3.90+/-0.04", "3.550+/-0.035", "3.250+/-0.033"],'B': [0.04175, 0.03800, 0.03490]})

df=df.replace("+/-", "±", regex=False)

print(df)

df.to_latex('232_a.txt', index=False, encoding="ISO-8859-1")

I'd appreciate any kind of help.我会很感激任何帮助。 Thanks in advance!提前致谢!

You may use您可以使用

df['A'] = df['A'].str.replace("+/-", "±", regex=False)

Output: Output:


    A   B
0   3.90±0.04   0.04175
1   3.550±0.035     0.03800
2   3.250±0.033     0.03490

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM