
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Write a program in Python to print dataframe rows as orderDict with a list of tuple values
Assume, you have a dataframe and the result for orderDict with list of tuples are −
OrderedDict([('Index', 0), ('Name', 'Raj'), ('Age', 13), ('City', 'Chennai'), ('Mark', 80)]) OrderedDict([('Index', 1), ('Name', 'Ravi'), ('Age', 12), ('City', 'Delhi'), ('Mark', 90)]) OrderedDict([('Index', 2), ('Name', 'Ram'), ('Age', 13), ('City', 'Chennai'), ('Mark', 95)])
Solution
To solve this, we will follow the steps given below −
Define a dataframe
Set for loop to access all the rows using df.itertuples() function inside set name=’stud’
for row in df.itertuples(name='stud')
Convert all the rows to orderDict with list of tuples using rows._asdict() function and save it as dict_row. Finally print the values,
dict_row = row._asdict() print(dict_row)
Example
Let’s check the following code to get a better understanding −
import pandas as pd Students = [('Raj', 13, 'Chennai', 80) , ('Ravi', 12, 'Delhi' , 90) , ('Ram', 13, 'Chennai', 95)] df = pd.DataFrame(Students, columns=['Name', 'Age', 'City', 'Mark']) print("DataFrame is:\n",df) for row in df.itertuples(name='stud'): dict_row = row._asdict() print(dict_row)
Output
DataFrame is: Name Age City Mark 0 Raj 13 Chennai 80 1 Ravi 12 Delhi 90 2 Ram 13 Chennai 95 OrderedDict([('Index', 0), ('Name', 'Raj'), ('Age', 13), ('City', 'Chennai'), ('Mark', 80)]) OrderedDict([('Index', 1), ('Name', 'Ravi'), ('Age', 12), ('City', 'Delhi'), ('Mark', 90)]) OrderedDict([('Index', 2), ('Name', 'Ram'), ('Age', 13), ('City', 'Chennai'), ('Mark', 95)])
- Related Articles
- Use a list of values to select rows from a Pandas DataFrame
- Write a program in Python to remove first duplicate rows in a given dataframe
- Python Program to print unique values from a list
- Python Program to print elements of a tuple
- Write a program in Python to print the ‘A’ grade students’ names from a DataFrame
- Python program to convert a list of strings with a delimiter to a list of tuple
- Write a Python program to find the mean absolute deviation of rows and columns in a dataframe
- Write a program in Python to select any random odd index rows in a given DataFrame
- Python program to print the keys and values of the tuple
- Python Program to print a specific number of rows with Maximum Sum
- Write a Python code to swap last two rows in a given dataframe
- Write a program in Python to print the length of elements in all column in a dataframe using applymap
- Write a program in Python to print numeric index array with sorted distinct values in a given series
- Python Program to Sort a Tuple By Values
- Python – To convert a list of strings with a delimiter to a list of tuple

Advertisements