jwillis0720
jwillis0720

Reputation: 4477

Iterate Pandas dataframe like a python dictionary

I have a unique index for each row, so my dataframe looks like this -

>>>df.head()
    Alignment   Sequence    Sequence2b  Post2b  SequenceLength  Sequence2bLength    Name    Year    Clade   Country Patient medoid
index                                               
44644   MRV-KET----QMNWP---N----------L---W---K-------...   MRVKETQMNWPNLWKWGTLIFGLVIICSAADNLWVTVYYGVPVWRD...       AGY 778 0   AA111a_WG3  2009    01_AE   TH  AA111   NA
3616    ------T----QMNWQ---H----------L---W---K-------...   TQMNWQHLWKWGTLILGLVIICSASNLWVTVYYGVPVWKDADTTLF...   AVVKINENATLDDTSY    LKN 822 16  041_WK32    -   B   CH  041 NA
36025   MRV-KET----QMSWP---N----------L---W---K-------...   MRVKETQMSWPNLWKWGTLILGLVIMCSASDNLWVTVYYGVPVWRD...   D   NTS 843 1   AE_Env_CR11_Jan09D  2009    01_AE   TH  CR11    NA
42027   MRV-KGI----RKNCQ---H----------L---W---R-------...   MRVKGIRKNCQHLWRWGIMLLGMLMICSTAEKLWVTVYYGVPVWRE...   DADEIHNDDNSS    SCY 862 12  CR0058S 2002    B   US  -   NA
31626   MRV-RGI----PRNYP---Q----------W---W---I-------...   MRVRGIPRNYPQWWIWGILGFWMIIICRVMESSWVTVYYGVPVWTD...   DAIPLNGNSSNSSSNSSEY LIN 852 19  03ZASK006B2 2003    C   ZA  SK006   NA

Is there a way to iterate it easily sort of like a key,value pair like a dictionary like this?

for index in df:
   print index, df[index]['Name']
   break
 >>>44644 : {Name:AA11a_WG3}

Here is what I have come up with, but I'm not sure if this is the most "pythonic" way.

 for index in df.index:
     print index, df[df.index == index].Name

The df.to_dict does not work since it removes the index.

Upvotes: 1

Views: 8129

Answers (2)

Afshin Amiri
Afshin Amiri

Reputation: 3583

If you (like me) want to iterate over rows and access each row as a dictionary, do this:

for item in df.to_dict(orient='records'):
     print(item)

using this you can access each cell by column_name.

Upvotes: 1

Alexander
Alexander

Reputation: 109756

You probably want to use iterrows().

for idx, row in df.iterrows():
     print((idx, row.Name))

Upvotes: 8

Related Questions