简体   繁体   中英

How to access unnamed tuple index in python

I have the following code:

import io
import pandas as pd

STARTLINES = """
r1: idswam1, 754527542, a396, 3, IR
r2: cbskuf5, 986435614, a952, 9, CH
r3: bksfsa3, 678934987, a396, 4, US
"""

STOPLINES = """
r1: idswam1, 876935874, a396, 3, IR
r2: bksfsa3, 723468723, a396, 4, US
r3: cbskuf5, 993456789, a952, 9, CH
"""

dfstart = pd.read_csv(io.StringIO(STARTLINES),header=None,sep=r"\s*[#:,]", engine='python')
dfstop = pd.read_csv(io.StringIO(STOPLINES),header=None,sep=r"\s*[#:,]", engine='python')

dfstart.drop(dfstart.columns[0],axis=1,inplace=True)
dfstop.drop(dfstop.columns[0],axis=1,inplace=True)

for row in dfstart.itertuples():
   print (row)

The output I get is:

Pandas(Index=0, _1=' idswam1', _2=754527542, _3=' a396', _4=3, _5=' IR')
Pandas(Index=1, _1=' cbskuf5', _2=986435614, _3=' a952', _4=9, _5=' CH')
Pandas(Index=2, _1=' bksfsa3', _2=678934987, _3=' a396', _4=4, _5=' US')

How do I access data from this data structure for each row? I've tried using _1 and '_1' but all give errors. Failed attempts to query have included:

results=dfstart.row[_1].isin(dfstop.row[_1]) 

My goal is to check for each row in dfstart if the values in dfstart[[_1,_3]] are present on a line in dfstop[[_1,_3]] and then store dfstart[_1],dfstart[_3],dfstart[_5] in a new dataframe.

As per Pandas itertuples documentation , this is by design.

pandas.DataFrame.itertuples yields namedtuples , which values are accessible by using integers, like this:

for row in dfstart.itertuples():
    print(row)
    print(row[1], row[2], row[3], row[4], row[5])
    
    # Pandas(Index=0, _1=' idswam1', _2=754527542, _3=' a396', _4=3, _5=' IR')
    # idswam1 754527542  a396 3  IR

    # Pandas(Index=1, _1=' cbskuf5', _2=986435614, _3=' a952', _4=9, _5=' CH')
    # cbskuf5 986435614  a952 9  CH

    # Pandas(Index=2, _1=' bksfsa3', _2=678934987, _3=' a396', _4=4, _5=' US')
    # bksfsa3 678934987  a396 4  US

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM