如何迭代pandas dataframe列中的元素?

时间:2017-09-25 13:41:23

标签: python python-3.x pandas

如何找出pandas数据框列中的每个元素? 我想做这样的事情:

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

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

数据框如下:

     USERCODES    NAMES
0    key00        ab00
1    key01        cd01
2    key02        ef02

1 个答案:

答案 0 :(得分:0)

对于列中的每个值,使用Series.apply

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

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

一行:

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