简体   繁体   English

dataframe 清理:从 pandas- 的列中删除特定数量的零

[英]dataframe cleaning: removing a specific number of zeros from a column in pandas-

I need help removing unwanted zeros from a data frame.我需要帮助从数据框中删除不需要的零。 The column values are integers and I'm not exactly sure how to remove the zeros from the 'Year' column.列值是整数,我不确定如何从“年”列中删除零。 As shown in the image below, (highlighted in blue), I'm trying to remove the zeros after '2014'.如下图所示(以蓝色突出显示),我正在尝试删除“2014”之后的零。 (there are a few other values like this in this column) (此列中还有其他一些类似的值)

数据集截图

You can filter first 4 values in column Year by converting to strings with str[:4] :您可以通过使用str[:4]转换为字符串来过滤Year列中的前 4 个值:

df['Year'] = df['Year'].astype(str).str[:4].astype(int)

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

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