我有一个数据框,而我想要做的基本上是在同一位置将成败团队的得分制成表格。我试图放置一个lambda函数,但没有成功。我当前拥有的数据框是第一个,我想以第二个问题的形式创建一个数据集。谢谢。
GameId Team Home Score
1 Spirit 1 81
1 Rockers 0 66
2 Lightning 1 73
2 Flames 0 82
Game ID Home Team Away Team Home Score Away Score
1 Spirit Rockers 81 66
2 Lightning Flames 73 82
答案 0 :(得分:4)
尝试一下:
输入:
import pandas as pd
raw_df = pd.DataFrame({"GameId": [1, 1, 2, 2],
"Team": ["Spirit", "Rockets", "Lighting", "Flames"],
"Home": [1, 0, 1, 0],
"Score": [81, 66, 73, 82]})
print(raw_df)
输出:
GameId Team Home Score
0 1 Spirit 1 81
1 1 Rockets 0 66
2 2 Lighting 1 73
3 2 Flames 0 82
输入:
raw_df.loc[:, "Home"] = raw_df.Home.map({
1: "Home",
0: "Away"
})
result = raw_df.pivot_table(index=["GameId"],
columns=["Home"],
values=["Team", "Score"],
aggfunc={"Team": lambda team: " ".join(team.tolist()),
"Score": lambda score: score})
result = result.sort_index(axis="columns", level=[0, "Home"], ascending=False)
result.columns = [' '.join(reversed(col)) for col in result.columns]
print(result)
输出:
Home Team Away Team Home Score Away Score
GameId
1 Spirit Rockets 81 66
2 Lighting Flames 73 82
答案 1 :(得分:2)
import pandas as pd
df=pd.DataFrame({'GameId':[1,1,2,2],'Team': ['Spirit','Rockers','Lighting','Flames'],'Home':[1,0,1,0],'Score':[81,66,73,82]})
merge=pd.merge(df,df,left_on='GameId',right_on='GameId')
merge=merge[merge['Home_x']!=merge['Home_y']]
merge=merge.drop_duplicates(subset=['GameId'])
merge=merge[['GameId','Team_x','Team_y','Score_x','Score_y']]
merge.columns=['GameId','Home Team','Away Team','Home Score','Away Score']
说明:我正在使用pd.merge()执行自连接。之后,我要在主场和客场列中删除具有相同团队名称的行。之后,在gameId上删除重复项,然后选择所需的列并重命名
答案 2 :(得分:2)
首先使用async function fetchAllIdsInGroup(){
let groupIds = [];
let groupNumber = document.querySelector('#groupNumber').value;
await fetch('http://localhost:1234/allIdsInGroup/' + groupNumber)
.then(response => response.json())
.then((data) => {
data.forEach(element => {
groupIds.push(element['_id'])
});
});
return groupIds;
}
async function printAllIdsInGroup(){
let testArray = ['a', 'b', 'c'];
console.log(testArray[0]); // this works
// outputs
// a
let groupIds = await fetchAllIdsInGroup();
console.log(groupIds[0]);
}
,然后执行一些列表理解,以将元组中的列重命名为所需的名称(由于将.pivot
设置为枢轴时将其设置为列,因此这些列为元组)。 Home
将名称从将团队元组加入列表理解中时,从团队到家团队。
[::-1]
输出:
df = pd.pivot(df, columns='Home', values=['Team','Score'], index='GameId').reset_index()
df.columns = [' '.join(str(s).strip().replace('1', 'Home').replace('0', 'Away') for s in col[::-1]) for col in df.columns]