[英]How to find a particular word in a csv file in a particular column with pandas
[英]How to delete a particular column in csv file without pandas library
我正在嘗試刪除 csv 文件中的特定列。
CSV 文件:
Name,Age,YearofService,Department,Allocation
Birla,49,12,Welding,Production
Robin,38,10,Molding,Production
我正在嘗試刪除具有 header“部門”和“分配”列的整個列。
我的代碼:
with open(input.csv,'r') as i:
with open(output.csv,'w',new line='') as o:
reader=csv.reader(i)
writer = csv.writer(o)
for row in reader:
for i in range(len(row)):
if row[i]!="Department" and row[i]!="Allocation":
writer.writerow(row)
我的 output:
Name
Birla
Robin
Age
49
38
YearofService
12
10
預期 output:
Name,Age,YearofService
Birla,49,12
Robin,38,10
我們不能保證部門和分配將在 header position “3”和“4”列中。 那就是你正在使用迭代通過行的長度
你可以寫這樣的東西(但還是用 pandas 更好):
import csv
def delete_cols(file: str, cols_to_delete: list):
cols_to_delete = set(cols_to_delete)
with open(file) as file, open('output.csv', 'w') as output:
reader = list(csv.reader(file))
headers = reader[0]
indexes_to_delete = [idx for idx, elem in enumerate(headers) if elem in cols_to_delete]
result = [[o for idx, o in enumerate(obj) if idx not in indexes_to_delete] for obj in reader]
writer = csv.writer(output)
writer.writerows(result)
delete_cols('data.csv', ['Department', 'Allocation'])
文件output.csv
:
Name,Age,YearofService
Birla,49,12
Robin,38,10
在這種情況下, csv.DictReader
和csv.DictWriter
類非常方便:
import csv
with open("input.csv") as instream, open("output.csv", "w") as outstream:
# Setup the input
reader = csv.DictReader(instream)
# Setup the output fields
output_fields = reader.fieldnames
output_fields.remove("Department")
output_fields.remove("Allocation")
# Setup the output
writer = csv.DictWriter(
outstream,
fieldnames=output_fields,
extrasaction="ignore", # Ignore extra dictionary keys/values
)
# Write to the output
writer.writeheader()
writer.writerows(reader)
筆記
對於輸入,每一行將是一個字典,例如
{'Name': 'Birla', 'Age': '49', 'YearofService': '12', 'Department': 'Welding', 'Allocation': 'Production'}
對於 output,我們刪除那些我們不需要的列(字段),見output_fields
extraaction
參數告訴DictReader
忽略字典中的額外鍵/值
為了從 CSV 文件中刪除列,我們需要
這是我從上面修改的代碼
import csv
with open("input.csv") as instream:
# Setup the input
reader = csv.DictReader(instream)
rows = list(reader)
# Setup the output fields
output_fields = reader.fieldnames
output_fields.remove("Department")
output_fields.remove("Allocation")
with open("input.csv", "w") as outstream:
# Setup the output
writer = csv.DictWriter(
outstream,
fieldnames=output_fields,
extrasaction="ignore", # Ignore extra dictionary keys/values
)
# Write to the output
writer.writeheader()
writer.writerows(rows)
最快最簡單的方法是在 excel 中打開它並刪除你想要的列,我知道這不是你想要的,但這是我想到的第一個解決方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.