python - Python script to skip specific column in CSV files
问题描述
I have a Python code which filters the data according to specific column and creates multiple CSV files.
Here is my main csv file:
Name, City, Email
john cty_1 a@g.com
jack cty_1 b@g.com
...
Ross cty_2 c@g.com
Rachel cty_2 d@g.com
...
My python logic currently creates separate csv for separate city. Existing python logic is:
from itertools import groupby
import csv
with open('filtered_final.csv') as csv_file:
reader = csv.reader(csv_file)
next(reader) #skip header
#Group by column (city)
lst = sorted(reader, key=lambda x : x[1])
groups = groupby(lst, key=lambda x : x[1])
#Write file for each city
for k,g in groups:
filename = k[21:] + '.csv'
with open(filename, 'w', newline='') as fout:
csv_output = csv.writer(fout)
csv_output.writerow(["Name","City","Email"]) #header
for line in g:
csv_output.writerow(line)
Now, I want to remove the "City" Column on each new CSV files.
解决方案
Then try to import like:
df = pd.read_csv('filtered_final.csv', usecols=['Name','Email'])
推荐阅读
- amazon-cognito - 在一段时间不活动后自动禁用已确认的 AWS Cognito 用户账户
- c# - 根据工作日安排 Azure Function
- amazon-web-services - AWS - Boto -第一次服务后停止服务
- c# - 如何创建只能输入数字的文本框?
- android - libwebviewchromium.so 在 AndroidTV 上崩溃,没有错误消息或堆栈跟踪
- python - 如何查询以便显示模型的字段而不是实际模型
- android - 如何将 Firebase 实时数据库与 RxJava 结合使用
- cordova - 如何在我的 ionic 应用程序中安装本机运行包?
- tensorflow - 我可以从具有动态大小的 TensorArray 中删除特定元素吗?
- pdf - 如何在高内存的 VBS 中运行命令