簡體   English   中英

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

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

我正在學習 Python Pandas,我有一個任務是根據 .csv 文件做一個生成器。 生成器必須實現為 function,它在 for 循環中調用並從第一列、第二列和最后一列返回值。 要從 function 返回值,我必須使用 yield 關鍵字。 我寫了這樣的東西:

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

但是我在這里不知道如何將其實現為 for 循環。 你對我有什么建議嗎? 我如何將產生的值存儲在元組或列表中?

# 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