简体   繁体   English

如何从 CVS 文件的数据列中删除最后一个字符?

[英]How to remove the last character from a column of data from a CVS file?

I am trying to do a linear relation using a csv file with 3 columns of data.我正在尝试使用具有 3 列数据的 csv 文件进行线性关系。 Unfortunately I am unable to read the 3rd (last column as int because it has an ';' in the end of each value. For example 213124121; , 1231231; etc.不幸的是,我无法读取第 3 列(最后一列为 int,因为它在每个值的末尾都有一个';' 。例如213124121;1231231;等。

data["the price;"] = df['the price;'].astype('int64') 

Error:错误:

ValueError: invalid literal for long() with base 10: '399900; '

How can I remove the ';'我怎样才能删除';' for the whole column?整个专栏?

You can simple change df['the price;'] to df['the price;'][:-2]您可以简单地将df['the price;']更改为df['the price;'][:-2]

it called string slicing in python, you can read more about it in here它在 python 中称为字符串切片,您可以在此处阅读有关它的更多信息

Thanks @ stovfl to spot a mistake in my answer感谢@stovfl在我的回答中发现错误

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

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