繁体   English   中英

如何在 python pandas 中将不带前导零和 + - 的字符串格式化为 integer?

[英]How to format string without leading zeros and + - as integer in python pandas?

我在 pandas dataframe 中有一列带有字符串的行,如下所示:

+0094
-0082

我将如何替换列中的所有值,以便它们的格式如下:

 94
-82

pandas足够聪明,可以通过数据帧的astype方法来解决。

所以你的方法是:

df['col of interest'] = df['col of interest'].astype(int)

我建议使用pd.to_numeric因为它提供了比.astype更大的灵活性:

df['col'] = pd.to_numeric(df['col'])

暂无
暂无

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

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