简体   繁体   中英

python - How to set column as index in a DataFrame

Given below is my current pandas DataFrame:

         Balance before Salary   Salary
Month                                 
Jun-18                  27.20  15300.0
Jul-18                  88.20  15300.0
Aug-18                 176.48  14783.0
Sep-18                  48.48  16249.0
Oct-18                 241.48  14448.0
Nov-18                  49.48  15663.0

is it possible to convert the above DataFrame in the below format?

                        Month1  Month2  Month3  Month4  Month5  Month6
Balance before Salary   27.2    88.2    176.48  48.48   241.48  49.48
Salary                 15300    15300   14783   16249   14448   15663

Code

df = pd.DataFrame(salary_List)
newdf = df.groupby('date').sum()
newdf = df.groupby(pd.Grouper(key='date', freq='1M')).sum()
newdf.index = newdf.index.strftime('%b-%y')
newdf.index.name = 'Month'

Can anyone please help me on this?

I think you need transpose by T and then if necessary change column names add list comprehension:

Notice:
Double groupby+sum is not necessary, once is enough, because aggregate same aggregate function, here sum .

df = pd.DataFrame(salary_List)

newdf = df.groupby(pd.Grouper(key='date', freq='1M')).sum().T
#python 3.6+
newdf.columns = [f'Month{x}' for x in range(1, len(newdf.columns) + 1)]

#python bellow
#newdf.columns = ['Month{}'.format(x) for x in range(1, len(newdf.columns) + 1)]
print (newdf)
                        Month1   Month2    Month3    Month4    Month5  \
Balance before Salary     27.2     88.2    176.48     48.48    241.48   
Salary                 15300.0  15300.0  14783.00  16249.00  14448.00   

                         Month6  
Balance before Salary     49.48  
Salary                 15663.00  

What you want to do is called 'transpose'. You can get the transposed dataframe by calling the df.transpose() function. So for you simply :

df = pd.DataFrame(salary_List)
newdf = df.transpose()

Then with a simple loop you may change the column names to the ones yoyu want.

You an also get the Month and Year(for better clarity) and then transpose:

resetting the index to get the Month as a column:

df1 = df1.rename_axis('Month').reset_index()
df1.drop([0],inplace=True)

Then:

df['Month'] = df['Month'].apply( lambda x : pd.to_datetime(x).strftime('%b %Y')) # gets name of month and year
df_new = df1.T #transpose and save to new df
df_new.columns = df_new.iloc[0] # set columnnames as monthnames
df_new.drop('Month',inplace=True) # drop the extra row Month

Output:

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM