簡體   English   中英

在 pandas dataframe 中迭代保存輸出

[英]Iteratively saving outputs in a pandas dataframe

我的數據集包含一個列,其中包含我想在 for 循環中檢查的名稱:

Name      Age

John      32
Luke      23
Christine  54
Mary      39
AnneMarie  42
Eoin      23

我需要通過一個生成一對('name',score)的網站來檢查它們,其中score是一個數字。 這對來自以下代碼(它不能工作,因為它只是為了顯示我如何在我的數據框中獲得我想要的數據而提取的)

for name in df['Name']: 

   # missing code
    for c in zip(names, scores):
        print(c)

例如,當name = John時, c給我以下 output:

('Julie', 6.7)
('Michael', 3.4)
('John John', 3.1)
('Ludo', 3.0)
('Chris', 3.0)

name = Luke時, c給了我以下 output:

('Mary', 2.7)
('Michael', 2.1)
('Bill', 3.5)
('Jess', 3.2)

等等。

我想在我的 dataframe 中添加這些信息,以便獲得這樣的信息:

 Name      Age                  Friends                        Score
    
    John      32     [Julie, Michael, John John, Ludo, Chris]  [6.7, 3.4, 3.1, 3.0, 3.0]
    Luke      23     [Mary, Michael, Bill, Jess]               [2.7,2.1, 3.5, 3.2]
    Christine  54
    Mary      39
    AnneMarie  42         ....
    Eoin      23

感謝您對此的幫助,關於如何通過使用“ Name ”列中每個名稱的結果 c 來獲得類似的 dataframe。

嘗試:

# add index here
for idx,name in df['Name'].iteritems(): 

    # missing code
    for c in zip(names, scores):
         print(c)

    df.loc[idx, 'Friends'] = names
    df.loc[idx, 'Score'] = scores

或者您可以更好地聚合所有名稱和分數並在 for 循環之后分配一次:

# initialization
name_lists, score_lists = [], []

for name in df['Name']: 

    # missing code
    for c in zip(names, scores):
         print(c)

    name_lists.append(names)
    score_lists.append(scores)

# update the data frame
df['Friends'] = name_lists
df['Score'] = score_lists

對於不太大的數據幀,后一個代碼比第一個代碼稍快。 對於更大的數據幀,重復append可能會非常慢。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM