Tuesday, 3 October 2023

How to Print DataFrame Row Wise in Pandas

Using iterrows() method of DataFrame, we can iterate over DataFrame rows as (index, Series) pairs.


print_dataframe_row_wise.py

import pandas as pd

# Create a sample DataFrame
data = {'Name': ['Krishna', 'Ram', 'Joel', 'Gopi', 'Jitendra', 'Raj'],
        'Age': [34, 25, 29, 41, 52, 23],
        'City': ['Bangalore', 'Chennai', 'Hyderabad', 'Hyderabad', 'Bangalore', 'Chennai']}

df = pd.DataFrame(data)

# Print the DataFrame row-wise
for index, row in df.iterrows():
    print( index, ". ",  row.tolist())

 

Output

0 .  ['Krishna', 34, 'Bangalore']
1 .  ['Ram', 25, 'Chennai']
2 .  ['Joel', 29, 'Hyderabad']
3 .  ['Gopi', 41, 'Hyderabad']
4 .  ['Jitendra', 52, 'Bangalore']
5 .  ['Raj', 23, 'Chennai']

 

  

Previous                                                 Next                                                 Home

No comments:

Post a Comment