这是我的"new_plates.csv“的样子-
TT59 TRI,"Black,Thomas,Mr"
HY63 YOJ,"Smith,Simon,Mr"
YY36 DPM,"Paul & Clare,Mr & Mrs"以下是Python代码-我首先将"plates.txt“文件转换为"plates.csv”文件,然后将未使用的列过滤为"new_plates.csv“并删除旧的"plates.csv”。这可以很好地工作,但是我想删除车牌号码中的空格,但将它们保留在名称中。什么不管用。
import os
import subprocess
import pandas
import pandas as pd
subprocess.call("sudo csvformat -t /home/pi/scripts/plates.txt >> /home/pi/scripts/plates.csv", shell=True)
f=pd.read_csv("plates.csv")
keep_col = [11,13]
new_f = f[keep_col]
new_f.to_csv("new_plates.csv", index=False)
new_f[1]= new_f[1].str.replace(" ", "")
subprocess.call("sudo rm /home/pi/scripts/plates.csv", shell=True)
print(new_f)我不知道该如何正确地写这行
new_f[1]= new_f[1].str.replace(" ", "")我想让它看起来像这样-不带空格的车牌号码
TT59TRI,"Black,Thomas,Mr"
HY63YOJ,"Smith,Simon,Mr"
YY36DPM,"Paul & Clare,Mr & Mrs"谢谢
发布于 2019-06-24 21:51:45
使用str.split().apply("".join)
Ex:
df=pd.read_csv("plates.csv", names=["plates", "name"])
df["plates"] = df["plates"].str.split().apply("".join)
print(df)输出:
plates name
0 TT59TRI Black,Thomas,Mr
1 HY63YOJ Smith,Simon,Mr
2 YY36DPM Paul & Clare,Mr & Mrs发布于 2019-06-24 21:57:47
首先,不需要将txt转换为csv,因为pandas也可以处理txt。第二,
new_f[1] = [plate.replace(" ", "") for plate in new_f[1]]https://stackoverflow.com/questions/56738009
复制相似问题