Python - How to transpose dataframe in pandas without index?

Learn, how can we transpose dataframe in Python pandas without index? By Pranit Sharma Last updated : September 29, 2023

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.

Transpose is a concept of the matrix which we use to cross the rows and columns of the 2-dimensional array or a matrix or a DataFrame.

Problem statement

Given a Pandas DataFrame, we have to transpose it without index.

Transposing dataframe in pandas without index

To transpose dataframe in pandas without index, we will be having a DataFrame with different columns, we will first reset its index using any of the columns and then we will use pandas.DataFrame.transpose() method to perform the transpose operation

Let us understand with the help of an example,

Python program to transpose dataframe in pandas without index

# Importing pandas package
import pandas as pd

# Creating two dictionaries
d = {
    'Name':["Ram","Shyam",'Ghanshyam'],
    'Math':[80,77,83],
    'Chemistry':[82,87,93],
    'Physics':[70,67,63]
}

# Creating DataFrames
df = pd.DataFrame(d)

# Display the DataFrames
print("Original DataFrame 1:\n",df,"\n\n")

# Transpose of DataFrame
df.set_index('Name',inplace=True)

df.transpose()

# Display modified DataFrame
print("Modified DataFrame:\n",df)

Output

The output of the above program is:

Example: Transpose dataframe in pandas without index

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.