Pandas transpose one column

Pandas transpose one column:
To transpose one column in pandas, you can use the transpose() function.

Here’s an example:

      import pandas as pd
      
      # Create a DataFrame
      df = pd.DataFrame({'Names':['John', 'Sam', 'Alice'],
                         'Ages':[25, 28, 30]})
                         
      # Transpose the 'Names' column
      transposed_df = df['Names'].transpose()
      
      print(transposed_df)
    

This will output:

      0    John
      1    Sam
      2    Alice
      Name: Names, dtype: object
    

In this example, we created a DataFrame with two columns: ‘Names’ and ‘Ages’. We then used the transpose() function to transpose the ‘Names’ column, resulting in a Series with the transposed values.

Leave a comment