No items found.
Analysis

Transposing Tables Using Python Pandas

This post was originally published on the Periscope Data Community. To see more content like this and to communicate with data experts like you, join the community.

Pandas makes transposing tables beautifully simple! Below are a couple examples using the example SQL output below:

This first Python snippet allows you to define your own column headers:

# SQL output is imported as a pandas dataframe variable called "df"
import pandas as pd
df2=df.T
df2.columns=['label 1', 'label 2','label 3','label 4']
# Use Periscope to visualize a dataframe or an image by passing data to periscope.output()
periscope.output(df2)

And this Python snippet makes the first row your column headers

# SQL output is imported as a pandas dataframe variable called "df"
import pandas as pd
df2=df.T
header=df2.iloc[0]
df2=df2[1:]
df2.columns=header
# Use Periscope to visualize a dataframe or an image by passing data to periscope.output()
periscope.output(df2)

Interested in learning about pivoting tables in Python or R? Check out our post here.

Tags: 

Want to discuss this article? Join the Periscope Data Community!

Neha Kumar
Neha is passionate about creating powerful data visualizations, spreading awareness of analytics best practices and learning about the latest and greatest advancements in data science. She strives to leverage multiple programming languages for analyses that are more efficient and effective than current methodologies.