[英]Formatting a csv file
I have a csv file with no formatting. 我有一个没有格式的CSV文件。 I need to format the csv file.
我需要格式化csv文件。
import pandas
df = pandas.read_csv('abc.csv')
df['speed'] = df['speed'].map('{:06,.2f}'.format)
df.to_csv("FINAL.csv")
Error: 错误:
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
File "C:\Python27\ArcGIS10.4\lib\site-packages\pandas\core\series.py", line 2009, in map
mapped = map_f(values, arg)
File "pandas\src\inference.pyx", line 1064, in pandas.lib.map_infer (pandas\lib.c:58525)
ValueError: Unknown format code 'f' for object of type 'str'
For Example: 例如:
df['speed'] = [54,156,3]
Expected output: 预期产量:
speed
054.00
156.00
003.00
Remove the comma in your string formatting and it should work. 删除您的字符串格式中的逗号,它应该可以工作。
import pandas as pd
df = pd.DataFrame([54,156,3])
df[0].map('{:06.2f}'.format)
output: 输出:
054.00
156.00
003.00
Here's a reasource for string formatting: 这是字符串格式化的原因:
https://pyformat.info/ https://pyformat.info/
Edit: 编辑:
It sounds like this is a problem of messy data. 听起来这是数据混乱的问题。 I suggest trying the following options when reading your csv file:
我建议您在读取csv文件时尝试以下选项:
df = pandas.read_csv('abc.csv', dtype=object)
df['speed'] = df['speed'].apply(pd.to_numeric, errors='coerce')
df['speed'] = df['speed'].map('{:06.2f}'.format)
errors='coerce'
will try to convert to float, and return NaN
if it fails to do so. errors='coerce'
将尝试转换为float,如果不这样做则返回NaN
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.