![](/img/trans.png)
[英]How to perform operations on rows of Dataframes which contain elements linked to a value in another Dataframe in Python?
[英]How to string match elements in arrays to index in another dataframe and perform operations?
我有一個包含數組的熊貓系列,它看起來像:
[array(['Person A', 34.0], dtype=object),
array(['Person C', 30.0], dtype=object)]
我有另一個數據框,其中包含有關數據庫中所有人的信息,如下所示:
Index Score_week41 Score_week42 Score_week43 Score_week44 Score_week45
Person A 6 2 3 4 7
Person B 0 3 4 2 1
Person C 1 2 4 0 8
如何將數組中的人與數據框中的人進行匹配? 我還希望能夠指定一個范圍,例如 'Score_week42':'Score_week44',其中顯示指定范圍內匹配人員的平均分數。
例如,輸出可能如下所示:
[array(['Person A', 34.0, 3], dtype=object),
array(['Person C', 30.0, 2], dtype=object)]
其中“3”和“2”分別是 A 和 C 在第 42 周和第 44 周之間的平均分數。
如果您想使用新列進行擴展,將這些系列統計信息存儲在數據框中會更方便
# your dataframe
all_data = pd.read_csv("people.csv", index_col="Index")
# your previous series
people_stat = pd.DataFrame([np.array(['Person_A', 34.0], dtype=object), np.array(['Person_C', 30.0], dtype=object)], columns=["People", "Number"]).set_index("People")
people_stat["mean"] = all_data.loc[people_stat.index, "Score_week42":"Score_week44"].mean(1)
print(people_stat)
輸出:
Number mean
People
Person_A 34.0 3.0
Person_C 30.0 2.0
人.csv
Index,Score_week41,Score_week42,Score_week43,Score_week44,Score_week45
Person_A,6,2,3,4,7
Person_B,0,3,4,2,1
Person_C,1,2,4,0,8
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.