Transposing a dataframe in R is as simple as 2 quick lines of code. The first is to actually transpose the table, and the second is to name your column headers.
Below is the dataframe we will be transposing. This is pulled from using the head() command on the build in cars dataset
The R snippet below transposes the data. The last line, periscope.output(df2) passes the dataframe back into Periscope Data so you can use the built-in charting options.
library(data.table) #Transpose dataframe. Pass through df2 into periscope.table() to view transposed table df2 = transpose(df) colnames(df2) <- c("car1", "car2", "car3", "car4","car5", "car6") periscope.table(df2)
Yielding the result below:
Prefer Python? Scroll down!
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 Sisense for Cloud Data Teams to visualize a dataframe or an image by passing data to periscope.output() periscope.table(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 df2=df2[1:] df2.columns=header # Use Sisense for Cloud Data Teams to visualize a dataframe or an image by passing data to periscope.output() periscope.table(df2)
Interested in learning about pivoting tables in Python or R? Check out the post here for more information.
Please sign in to leave a comment.