Data is generated, casually could transform CodePudding user response:
The import pandas as pd The import numpy as np Import pandas. IO. Formats. Excel # set the format to use The import xlsxwriter # set the format to use, PIP install xlsxwriter Chengji=[[" a ", 95, 100, 99], [" a ", 98, 99, 100], [" a ", 95, 98, 88], [" b ", 98, 97, 87], [" b ", 90, 96, 85], [" b ", 94, 93, 91], [" a ", 99, 93, 91]] Data=https://bbs.csdn.net/topics/pd.DataFrame (chengji, columns=[' categories', "language", 'mathematics',' political ']) Print (data index, data. The columns) # vertical sequence, cross headings Print (" * * "50," raw data ") Print (data) Data1=data. Set_index (' categories') Print (data1) # data1=data. Groupby (' categories') # a single group, available ([...] [...]. ) more columns group # data2=data1 [' language '] agg ([np sum, np. Mean]) 50 # print (" * * ", "Chinese, after using a classification NP sum, average") # print (data2) Lb=set (data) [r]. 'categories' tolist ()) Print (lb) Data2=pd. DataFrame () For LBX in lb: Nr=data1 [data1. Index==LBX] [r]. "Chinese" tolist () For num, NRX enumerate in (nr) : Data2. Loc [num, LBX]=NRX Print (data2) CodePudding user response:
import pandas as pd Data=https://bbs.csdn.net/topics/[[' A ', 329119], [' A ', 303068], [' A ', 282649], [' B ', 170843], [' B ', 147040], [' B ', 141867], [' B ', 119436], [' C ', 110657], [' C ', 106842], [' C ', 100016], [' C ', 106842], [' C ', 89662]] Df=pd DataFrame (data=https://bbs.csdn.net/topics/data, columns=[' Name 'and' data ']) B=df [' Name ']. Unique () D={} For I in range (0, len (b)) : D [b] [I]=df. Loc [df. Name==b [I], 'Data']. Reset_index (drop=True) Result=pd. DataFrame (d, the columns=b) Print (result)