问题
I am importing an excel document and creating a dataframe, df3. I want to group by only Name
. The other uplicate data should reflect as shown in the output.
Df3 =pd.read_excel('stats')
print (df3)
Name ID Month Shift
Jon 1 Feb A
Jon 1 Jan B
Jon 1 Mar C
Mike 1 Jan A
Mike 1 Jan B
Jon 1 Feb C
Jon 1 Jan A
Output Required:
I want to have output like as below in the same format and will save in excel. Please help me on same as I'm stuck here. Note (Month must be ascending order)
Will be greatfull for help and support .
回答1:
Here is how you can do it.
Solution:
Input:
Name,ID,Month,Shift
Jon,1,Feb,A
Jon,1,Jan,B
Jon,1,Mar,C
Mike,1,Jan,A
Mike,1,Jan,B
Jon,1,Feb,C
Jon,1,Jan,A
I have taken your file input as csv.
Code:
import pandas as pd
filePath="{YourFilePathForReadingcsv}"
df=pd.read_csv(filePath,sep=',')
df['MonthNumber']=pd.to_datetime(df['Month'],format='%b')
df.set_index(['Name','MonthNumber'],inplace=True)
df.sort_index(inplace=True)
df.to_excel("{filePath}\Jon.xlsx")
Output: Please remove column MonthNumber while writing it.
回答2:
I have created a sample dataframe as yours. You can load your data instead of this.
Code:
import pandas as pd
df = pd.DataFrame({'Name':['Jon','Jon','Jon','Mike','Mike','Jon','Jon'],'ID':[1,1,1,1,1,1,1], 'Month':['Feb','Jan','Mar','Jan','Jan','Feb','Jan'], 'Shift':['A','B','C','A','B','C','A']})
#display data
df
Loading data from Excel. I have created an excel file as you need
Now Reading Data From Excel:
#path is your path of excel file like "C:\test\"
df = pd.read_excel(path+'statsyour.xlsx', sheet_name='Sheet5')
#display data
df
Now we are going to transform the table as you needed(Sorting by Name and Month)
Code
df['Month'] = pd.Categorical(df['Month'],categories=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],ordered=True)
df = df.sort_values(['Name','Month']).reset_index(drop=True)
#display sorted table data
df
I hope this would be helpful.. (Write comments if any problem persist) :)
来源:https://stackoverflow.com/questions/62613892/not-able-to-groupby-by-one-level-in-my-dataframe-by-pandas