Person occupation partner favorite_color
Anthony student Alissa pink
Alissa professor Anthony yellow
Jordan professor Khan green
Khan student Jordan orange
Benny professor Anastasia blue
Anastasia student Benny red
McCurdy professor Anastasia purple
Alicia student Benny black
Janet student Benny white
你好,我想知道是否有可能过滤和更改我的整个csv文件,以便每个教授都能在另一列中列出他的学生,例如:
V_I_P color sidekick sidekick_color
**Alissa** yellow
Anthony pink
**Benny** blue Anastasia red
Alicia black
Janet white
等等…
所以如果这个人是教授,他在vip列,然后如果学生被分配给一个教授,他们进入助手列。是创建一个新的csv输出文件还是修改整个csv文件更好?csv模块是否足够?在互联网上,我只是不断寻找方法与熊猫模块
不需要Pandas使用Python的标准库。一个csv.DictWriter
可以帮上忙。也可以使用defaultdict
来帮助创建基于occupation
列的列表:
from collections import defaultdict
import csv
# Read the rows into lists based on the occupation
data = defaultdict(list)
with open('input.csv') as f_input:
csv_input = csv.DictReader(f_input)
for row in csv_input:
data[row['occupation']].append(row)
with open('output.csv', 'w', newline='') as f_output:
csv_output = csv.DictWriter(f_output, fieldnames=['V_I_P', 'color', 'sidekick', 'sidekick_color'])
csv_output.writeheader()
for professor in data['professor']:
person = professor['Person']
csv_output.writerow({'V_I_P' : f"**{person}**", 'color' : professor['favorite_color']})
for student in data['student']:
if student['partner'] == person:
csv_output.writerow({'sidekick' : student['Person'], 'sidekick_color' : student['favorite_color']})
输出CSV,包含:
V_I_P,color,sidekick,sidekick_color
**Alissa**,yellow,,
,,Anthony,pink
**Jordan**,green,,
,,Khan,orange
**Benny**,blue,,
,,Anastasia,red
,,Alicia,black
,,Janet,white
**McCurdy**,purple,,
我建议您print(data)
了解存储的内容。