Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Print pandas rows cells as string

I’m trying to print a data frame where each cell appears as a string:

Dataset

    a               b            c        
0 car        new york        queens  
1 bus        california      los angeles 
2 aircraft   illinois        chicago 
3 rocket     texas           houston  
4 subway     maine           augusta 
5 train      florida         miami 

Mon script:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

for index, row in df.iterrows():
    print(df["a"], "\n", testes["c"], "\n", testes["b"])

My output:

0 car

1 bus

2 aircraft

3 rocket

4 subway

5 train

Name: a, dtype: object

Good output:

car
queens 
new york

bus
los angeles
california

...

>Solution :

Looping is slow, but possible if use row Series:

for index, row in df.iterrows():
    print(row["a"], row["c"], row["b"], sep="\n")

Another idea is convert columns to numpy array:

for a, b, c in df[['a','b','c']].to_numpy():
    print(a, c, b, sep="\n")

Or zip:

for a, b, c in zip(df['a'],df['b'],df['c']):
    print(a, c, b, sep="\n")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading