我尝试使此代码有效,但它有错误:
for myname in mn:
mydf = df.loc[df.Teacher_name == myname]
mydf.to_excel(writer, sheet_name = myname)
writer.save()
File "<stdin>", line 4
writer.save()
^
SyntaxError: invalid syntax
进口=
import pandas as pd
writer =
writer = pd.ExcelWriter("MyData.xlsx", engine='xlsxwriter')
mn =
mn = df['name'].unique().tolist()
mn = ['Teacher A', 'Teacher B', 'Teacher C']
要使df使用此查询:
df = pd.read_csv('XXX.csv', names=['Teacher_name', 'Studen_name','Extra'])
df输出:
Teacher_name Student_name Extra
1 Teacher A Student 1 xxxx
2 Teacher A Student 2 xxxx
3 Teacher B Student 3 xxxx
4 Teacher C Student 4 xxxx