簡體   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