简体   繁体   中英

How do I iterate over elements in a pandas dataframe column?

How can I find out each element present in a column of a pandas dataframe? I would like to do something like this:

for item in column:
    print("usercode = " + item)

# OUTPUT:
# usercode = key00
# usercode = key01
# usercode = key02

The dataframe is like this:

     USERCODES    NAMES
0    key00        ab00
1    key01        cd01
2    key02        ef02

And for each value in column use Series.apply :

def f(item):
    print("usercode = " + item)

df['NAMES'].apply(f)
usercode = ab00
usercode = cd01
usercode = ef02

One line:

df['NAMES'].apply(lambda item: print("usercode = " + item))

usercode = ab00
usercode = cd01
usercode = ef02

Simpliest:

for item in df['NAMES']:
    print("usercode = " + item)

usercode = ab00
usercode = cd01
usercode = ef02

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