0

I have this pandas df which i imported from a csv:

df
0  0 apple  banana  orange                        dates apple  banana  orange
1  1      1       1      1     Friday, January 01, 2021      1       1      1
2  2      1       1      1   Saturday, January 02, 2021      2       2      2
3  3      1       1      1     Sunday, January 03, 2021      3       3      3
4  4      1       1      1     Monday, January 04, 2021      4       4      4
5  5      1       1      1    Tuesday, January 05, 2021      5       5      5
6  6      1       1      1  Wednesday, January 06, 2021      6       6      6
7  7      1       1      1   Thursday, January 07, 2021      7       7      7
8  8      1       4      1     Friday, January 08, 2021      8       8      8
9  9      1       1      1   Saturday, January 09, 2021      9       9      9

Is it possible for everything on the left to be grouped under fresh and everything on the right of the dates to be under column spoil in multiindex format. Such as, there is one column which contains [apple, banana, orange]. I want to do this because later when i set the date as index there would be no confusion as both sides of the column have the same names.

3 Answers 3

1
df.columns = pd.MultiIndex.from_arrays([['', '', 'fresh', 'fresh', 'fresh', '', 'spoil', 'spoil', 'spoil'],
                                        df.columns])

output:

         fresh                                              spoil                  
   0   0 apple banana orange                        dates   apple   banana   orange
0  1   1     1      1      1     Friday, January 01, 2021       1        1        1
1  2   2     1      1      1   Saturday, January 02, 2021       2        2        2
2  3   3     1      1      1     Sunday, January 03, 2021       3        3        3
3  4   4     1      1      1     Monday, January 04, 2021       4        4        4
4  5   5     1      1      1    Tuesday, January 05, 2021       5        5        5
5  6   6     1      1      1  Wednesday, January 06, 2021       6        6        6
6  7   7     1      1      1   Thursday, January 07, 2021       7        7        7
7  8   8     1      4      1     Friday, January 08, 2021       8        8        8
8  9   9     1      1      1   Saturday, January 09, 2021       9        9        9

NB. if you want to set_index('dates') do it before this operation, this will be easier

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your help. When i try to run i get this error: ValueError: all arrays must be same length
Check the number of columns in your real dataframe. Might be different?
Could you please also advise how i can get rid of the first 3 '0' columns ? Thanks
The first one is the index, it will go away if you reset_index('dates'). The other two are indeed columns you can remove them with df.iloc[:, 2:] or df.drop('0', axis=1) (if the zero is a string).
1

This may help df.columns.values[1] = "apple1" df.columns.values[2] = "banana1"

1 Comment

1

You can try:

# Get the column number of column `dates`
dates_loc = df.columns.get_loc('dates')

arrays = [['fresh'] * dates_loc + [''] + ['spoil'] * (len(df.columns) - dates_loc -1), df.columns.tolist()]

df.columns = pd.MultiIndex.from_arrays(arrays)



  fresh                                                        spoil                  
      0   0 apple banana orange                        dates   apple   banana  orange
0     1   1     1      1      1     Friday, January 01, 2021       1        1        1
1     2   2     1      1      1   Saturday, January 02, 2021       2        2        2
2     3   3     1      1      1     Sunday, January 03, 2021       3        3        3
3     4   4     1      1      1     Monday, January 04, 2021       4        4        4
4     5   5     1      1      1    Tuesday, January 05, 2021       5        5        5
5     6   6     1      1      1  Wednesday, January 06, 2021       6        6        6
6     7   7     1      1      1   Thursday, January 07, 2021       7        7        7
7     8   8     1      4      1     Friday, January 08, 2021       8        8        8
8     9   9     1      1      1   Saturday, January 09, 2021       9        9        9

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.