Skip to content
Related Articles
Open in App
Not now

Related Articles

Python Program for Column to Row Transpose using Pandas

Improve Article
Save Article
Like Article
  • Last Updated : 20 Jan, 2022
Improve Article
Save Article
Like Article

Given an Input File, having columns Dept and Name, perform an operation to convert the column values to rows. Name contains pipe separated values that belong to a particular department identified by the column Dept.

Attached Dataset: emp_data


dept, name
10, Vivek|John
20, Ritika|Shubham|Nitin
30, Vishakha|Ankit

dept, name
10, Vivek
10, John
20, Ritika
20, Shubham
20, Nitin
30, Vishakha
30, Ankit

Method 1: Pythonic Way


# Reading Data From the text
# file
data = pd.read_csv(r'GFG.txt')
# create new data frame with 
# split value columns separates
# data into three columns as per
# separator mentioned
new = data["name"].str.split("|",expand = True
# making separate first name column
# from new data frame assign columnn
# values to dataframe new columns
# named as name*
data["Name1"] = new[0]
data["Name2"] = new[1]
data["Name3"] = new[2]
# Dropping old Name columns 
data.drop(columns =["name"], inplace = True
# create separate dataframes with two
# columns id,name
d_name1 = data[['dept','Name1']]
d_name2 = data[['dept','Name2']]
d_name3 = data[['dept','Name3']]
# perform concat/unions operation for
# vertical merging of dataframes
# concatenate values of series into one
# series "name"
union_df['name'] = union_df['Name1'].astype(str)+union_df['Name2'].astype(str)+union_df['Name3'].astype(str)
# drop column names
# drop rows having empty values
# sort the dataframe data by dept values

Column to Row Transpose using Pandas

Note: Shortcoming of above method is when there are more than 3 names separated by |

Method 2: Exploring Pandas


emp_df = pd.read_csv(r'GFG.txt')
# split column data on basis of separator
# convert it into list using to_list
# stack method performs transpose operation
# to the data
emp_df1 = pd.DataFrame('|').to_list(),
                       index = emp_df.dept).stack()
emp_df1 = emp_df1.reset_index([0, 'dept'])
emp_df1.columns =['Dept', 'Name']


Column to Row Transpose using Pandas

Method 3: The Pandas way: explode()


df = pd.read_csv(r'GFG.txt')
# separate values using split()
# transpose is performed by explode 
# function explode function overcomes 
# the method1 shortcomings incase we
# have many columns we explode will do
# the task in no time and with no hassle
df1 = df.assign(name = df['name'].str.split('|')).explode('name')

Column to Row Transpose using Pandas

My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!