Hi everyone! I’m working with python in snowpark. I have this dataframe created using session.sql to pull my sql query and organize it. I’m able to select the columns I want. But how would I create a second row and organize column_6, column_7, column_8, column_9 to be in that second row and align them directly under the other columns. So should appear as column_6 in row 2 directly under column_1, column_7 in row 2 under column_2, etc. And add a row label into the dataframe. Lets call it Row_1, Row_2. Appreciate all help and guidance.
It may also be helpful to provide a schematic illustration of the intended dataframe layout.
As the data has been converted to a Pandas DataFrame via `to_pandas(), you can utilize Pandas capability to restructure the DataFrame. Thus, you may want to look into the pivot function and there’s a great in-depth tutorial in the article below:
There may be smarter ways, but a straighforward, brute force approach should work (with pandas, it may or may not make sense for snowpark).
Create a DataFrame with the first 5 columns. Add a column for the row label. Set a sequence of consecutive even numbers as the index, starting from 0.
Create another DataFrame with the columns 6 to 9. Rename the columns to mach the ones in the first DataFrame. Add a column for the row label. Set a sequence of consecutive odd numbers as the index, starting from 1.
Concatenate both DataFrames. Sort the result by index if necessary.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.