Converting Rows To Columns And Vice Versa With Pandas
Convert rows to columns or vice versa using pandas library in Python! Use melt() function to transform rows into columns and pivot() function to do the opposite. Example code included!
In Python, you can use the pandas library to work with tabular data, and the core data type in pandas is the DataFrame. Sometimes, when working with DataFrame data, you may need to convert rows to columns or columns to rows. Here is a simple example demonstrating how to achieve this using the pandas library. Create Table At first, lets’ create a new example DataFrame. import pandas as pd # create a new DataFrame df = pd.DataFrame({ 'name': ['John', 'Mary', 'Peter'], 'math': [80, 90, 70], 'english': [70, 85, 90], 'science': [75, 95, 80] }) print(df) # output: name...