2

my data

I want to transform/transpose this DF into an other DF with one row containing all the columns: for exemple i will have columns GearLeverPosition_v2_count, GearLeverPosition_v2_mean ... with the index 0 having the values for each one.

3
  • could you add in the question an example of the desired result. looks like you want a vector containing metrics (count, mean) for each column. Is that right? Commented Jun 1, 2022 at 8:49
  • yeah it's right i want only one raw and a lot of columns Commented Jun 1, 2022 at 8:58
  • then you don't want to transpose it. you are getting several answers that tell you how to transpose, since it's what you asked in the title. Commented Jun 1, 2022 at 10:20

4 Answers 4

4

Same as in numpy, to transpose a DataFrame in pandas you can:

df.T

or equivalently:

df.transpose()
Sign up to request clarification or add additional context in comments.

4 Comments

df.transpose() doesnt make it, i've already tried, i want to transform the whole data frame with one line and severals columns. my final df should have 8 columns having GearLeverPosition_v2 + prefix, same for other columns.
for now i have 226 columns, i want one line with 226 x 8 columns
Right, then you mean a pivot table
i tried with valueError : no groupe keys passed –
1

You can use transpose() function to transpose index and columns. Reflect the DataFrame over its main diagonal by writing rows as columns and vice-versa.

Comments

0
array([[1, 2, 3],
       [4, 5, 6],
       [1, 2, 3],
       [1, 2, 3],
       [1, 2, 3]])

# insert col in array
newCol = [0,0,0,0,0]
arrNew = (np.vstack([arr.transpose(), newCol])).transpose()

array([[1, 2, 3, 0],
       [4, 5, 6, 0],
       [1, 2, 3, 0],
       [1, 2, 3, 0],
       [1, 2, 3, 0]])

Comments

-1

There's a transpose function in pandas

https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.transpose.html?highlight=transpose#pandas.DataFrame.transpose

2 Comments

While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.
Posts should answer the question directly (in writing) rather than indirectly (in a link). Would you consider removing this post so it doesn't separate the actual answer (below) away from the question?

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.