简体   繁体   English

如何使用 Python Pandas 从 1 行中的特定列获取值?

[英]How to get value from specific column in 1 row using Python Pandas?

I am learning a Python Pandas and I have a task to do a generator based on a.csv file.我正在学习 Python Pandas,我有一个任务是根据 .csv 文件做一个生成器。 The generator has to be implemented as a function which is called in for loop and returnes value from the first one, second one and the last one column.生成器必须实现为 function,它在 for 循环中调用并从第一列、第二列和最后一列返回值。 To return value from function I have to use yield keyword.要从 function 返回值,我必须使用 yield 关键字。 I wrote something like this:我写了这样的东西:

def patient_reader(data_frame): #sa - still alive, a -age, ms - months survived
    sa = data_frame['still_alive']
    a = data_frame['age']
    ms = data_frame['months_survived']
    yield sa,a,ms

But I don't know here how to implement this into a for loop.但是我在这里不知道如何将其实现为 for 循环。 Do you have any sugestions for me?你对我有什么建议吗? How could I store yielded values in the tuple or list?我如何将产生的值存储在元组或列表中?

# try this
def patient_reader(data_frame): #sa - still alive, a -age, ms - months survived
    res_list = []
    for row in data_frame.iterrows():
        sa = row['still_alive']
        a = row['age']
        ms = row['months_survived']
        res_list.append((sa, a, ms))
    yield res_list

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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