1
我有2個不同的硬幣翻轉數據框。我想要的是找到兩件事情的函數:Python熊貓創建函數來計算n列的行的平均值
- 的平均分(滿分爲100%),其中頭= 1和尾= 0
- 他們打得獲得遊戲的數量是得分
是否有可能使函數動態爲n列?
import pandas as pd
import numpy as np
df=pd.DataFrame({'Users': [ 'Bob', 'Jim', 'Ted', 'Jesus', 'James'],
'Round 1': ['np.nan','H','np.nan','T','H'],
'Round 2': ['np.nan','H','H','H','T'],
'Round 3': ['np.nan','T','T','T','T'],
})
df2=pd.DataFrame({'Users': [ 'Boob', 'Paul', 'Todd', 'Zeus', 'Derrik'],
'Round 1': ['H','H','np.nan','T','np.nan'],
'Round 3': ['H','T','H','T','np.nan'],
'Round 5': ['H','T','H','T','np.nan'],
'Round 7': ['H','H','H','H','H'],
})
df = df.set_index('Users')
df2 = df2.set_index('Users')
print (df)
print (df2)
這裏是我的嘗試:
def score(data):
score_map = {'H':1, 'T':0}
data=data.replace(score_map)
data['average']=
data['rounds played']=
df=score(df)
我猜我不得不使用GROUPBY這是否可能
結果應該是這個樣子:
Round 1 Round 2 Round 3 Average Rounds played
Users
Bob np.nan np.nan np.nan NaN 0
Jim 1 1 0 0.66 3
Ted np.nan 1 0 0.5 2
Jesus 0 1 0 0.33 3
James 1 0 0 0.33 2
[5 rows x 3 columns]