2

How would you drop / reset the column axis to shift the data down causing the column headers to be something like [0, 1, 2, 3, 4, 5] then set column headers to df[5] values? I reset the index of the rows axis all the time but never had the need to do it to columns.

df = pd.DataFrame({'very_low': ['High', 'Low', 'Middle', 'Low'], '0.2': [0.10000000000000001, 0.050000000000000003, 0.14999999999999999, 0.080000000000000002], '0.1': [0.080000000000000002, 0.059999999999999998, 0.10000000000000001, 0.080000000000000002], '0.4': [0.90000000000000002, 0.33000000000000002, 0.29999999999999999, 0.23999999999999999], '0': [0.080000000000000002, 0.059999999999999998, 0.10000000000000001, 0.080000000000000002], '0.3': [0.23999999999999999, 0.25, 0.65000000000000002, 0.97999999999999998]})

      0   0.1   0.2   0.3   0.4 very_low
0  0.08  0.08  0.10  0.24  0.90     High
1  0.06  0.06  0.05  0.25  0.33      Low
2  0.10  0.10  0.15  0.65  0.30   Middle
3  0.08  0.08  0.08  0.98  0.24      Low

2 Answers 2

1

If I understood you correctly, something like this?

df2 = pd.concat([pd.DataFrame(df.columns).T, pd.DataFrame(df.values)], 
                ignore_index=True).iloc[:, :-1]

df2.columns = [df.columns[-1]] + df.iloc[:, -1].tolist()

>>> df2
  very_low  High   Low Middle   Low
0        0   0.1   0.2    0.3   0.4
1     0.08  0.08   0.1   0.24   0.9
2     0.06  0.06  0.05   0.25  0.33
3      0.1   0.1  0.15   0.65   0.3
4     0.08  0.08  0.08   0.98  0.24
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your help! Your answers are always spot-on. You really know your stuff.
0

I think this is what you want:

tdf = df.T
tdf.columns = tdf.iloc[5]
tdf.drop(tdf.tail(1).index,inplace=True)

>>> tdf
very_low  High   Low Middle   Low
0         0.08  0.06    0.1  0.08
0.1       0.08  0.06    0.1  0.08
0.2        0.1  0.05   0.15  0.08
0.3       0.24  0.25   0.65  0.98
0.4        0.9  0.33    0.3  0.24

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.