Home > other >  For two of the python programming problem
For two of the python programming problem

Time:09-18

Python topic for help
1. The "homework - raw scores. XLSX file" nickname for processing, will begin with sdufe nickname, sdufe 11 behind the name out behind the student id and student id, and test, discussion, three data into numerical scores, "-" into "0", wrote "export achievement. XLSX file," run results are as follows:
Original score for

2. A given film director, actor. XLSX file, statistics every actor in the movie list, write "cast in the movie. XLSX" excel file, run results as follows:
For a given file form

CodePudding user response:

Import XLRD library, to excel read line by line, simple judgment should go

CodePudding user response:

 import pandas as pd 

Df=pd read_excel (r "c: \ work - raw scores. XLSX")
Dfwhere=df [df [' students' nickname '] STR [5]=='sdufe']
Df_code=dfwhere [' students' nickname '] STR [this]
Df_name=dfwhere [' students' nickname '] STR/16:
Df_test=dfwhere [' test ']
Df_discuss=dfwhere [' discussion ']. Replace (" - ", '0')
Df_grade=dfwhere [' scores']

Dfnew=pd. DataFrame (list (zip (df_code df_name, df_test, df_discuss, df_grade)), the columns=[' student id ', 'name', 'test', 'discussion', 'performance'])
Dfnew. To_excel (r "c: \ export grades. XLSX", the index=False)


 df=pd read_excel (r "c: \ film director, actor. XLSX") 
D={}
For the index, row in df iterrows () :
The actor, movie=row [' actors'], row [' movie name ']
For n in the actor. The split (', ') :
If d.g et (n, 'f')=='f' :
[n]=d movie
The else:
If movie not in d [n] :
[n] +=", "+ d movie

Dg=pd. DataFrame ([(k, v) for k, v in d.i tems ()], the columns=[' actors', 'movie'])
Dg. To_excel (r "c: \ cast in the movie. XLSX", the index=False)

CodePudding user response:

refer to the second floor chuifengde response:
 import pandas as pd 

Df=pd read_excel (r "c: \ work - raw scores. XLSX")
Dfwhere=df [df [' students' nickname '] STR [5]=='sdufe']
Df_code=dfwhere [' students' nickname '] STR [this]
Df_name=dfwhere [' students' nickname '] STR/16:
Df_test=dfwhere [' test ']
Df_discuss=dfwhere [' discussion ']. Replace (" - ", '0')
Df_grade=dfwhere [' scores']

Dfnew=pd. DataFrame (list (zip (df_code df_name, df_test, df_discuss, df_grade)), the columns=[' student id ', 'name', 'test', 'discussion', 'performance'])
Dfnew. To_excel (r "c: \ export grades. XLSX", the index=False)


 df=pd read_excel (r "c: \ film director, actor. XLSX") 
D={}
For the index, row in df iterrows () :
The actor, movie=row [' actors'], row [' movie name ']
For n in the actor. The split (', ') :
If d.g et (n, 'f')=='f' :
[n]=d movie
The else:
If movie not in d [n] :
[n] +=", "+ d movie

Dg=pd. DataFrame ([(k, v) for k, v in d.i tems ()], the columns=[' actors', 'movie'])
Dg. To_excel (r "c: \ cast in the movie. XLSX", the index=False)

If a third party libraries use is openpyxl, should how to write

CodePudding user response:

Above is not used openpyxl ah, directly with the pandas

CodePudding user response:

reference 4 floor weixin_45903952 response:
no use openpyxl above ah, solved directly with pandas

Because the teacher leave homework let us use openpyxl libraries are required to write code

CodePudding user response:

Give you write an example, according to hoist drawing ticket myself writing the second:
 import openpyxl as oxl 

Wb=oxl. Load_workbook (r "c: \ work - raw scores. XLSX")
Sheet=wb. Active

WBN=oxl. Workbook ()
Sheetn=WBN. Active
Sheetn. Append ([' student id ', 'name', 'test', 'discussion', 'performance'])

For I in sheet. Rows:
V=I [0]. Value
If v [5]=='sdufe' :
[this], l=[v v [16:], [4]. I value, STR (I [5]. The value). The replace (" - ", '0'), [6]. I value]
Sheetn. Append (l)

WBN. Save (r "c: \ export grades. XLSX")

CodePudding user response:

The
refer to the original poster weixin_47190428 response:
python topic for help
1. The "homework - raw scores. XLSX file" nickname for processing, will begin with sdufe nickname, sdufe 11 behind the name out behind the student id and student id, and test, discussion, three data into numerical scores, "-" into "0", wrote "export achievement. XLSX file," run results are as follows:
Original score for

2. A given film director, actor. XLSX file, statistics every actor in the movie list, write "cast in the movie. XLSX" excel file, run results as follows:
For a given file form

Where is the code wrong??
The import openpyxl
The from openpyxl import Workbook
Def getActors (filename) :
Actors=dict (wb)=openpyxl. Load_workbook (filename) ws=wb. Worksheets [0]
For the index, the row in enumerate (ws) rows) :
If the index==0:
The continue filmName, actor=row [0]. The value, the row [2]. The value. The split (", ") for a actor in:
Actors [a]=actors. Get (a, the set ()) actors [a]. Add (filmName)
Return actors
Actors=getActors (' film director, actor. XLSX)
Actors=sorted (actors. The items (), key=lambda x: int ([0] x [2:]))
For the item in actors:
nullnullnullnullnullnullnullnullnullnullnull
  • Related