Pandas只能将大小为1的数组转换为Python标量
我有这个数据帧,Pandas只能将大小为1的数组转换为Python标量,python,pandas,Python,Pandas,我有这个数据帧,df\u pm: Player GameWeek Minutes \ PlayerMatchesDetailID 1 Alisson 1 90 2 Virgil van Dijk 1 90 3 Joseph
df\u pm
:
Player GameWeek Minutes \
PlayerMatchesDetailID
1 Alisson 1 90
2 Virgil van Dijk 1 90
3 Joseph Gomez 1 90
ForTeam AgainstTeam \
1 Liverpool Norwich City
2 Liverpool Norwich City
3 Liverpool Norwich City
Goals ShotsOnTarget ShotsInBox CloseShots \
1 0 0 0 0
2 1 1 1 1
3 0 0 0 0
TotalShots Headers GoalAssists ShotOnTargetCreated \
1 0 0 0 0
2 1 1 0 0
3 0 0 0 0
ShotInBoxCreated CloseShotCreated TotalShotCreated \
1 0 0 0
2 0 0 0
3 0 0 1
HeadersCreated
1 0
2 0
3 0
第二个数据帧,df\u melt
:
MatchID GameWeek Date Team Home \
0 46605 1 2019-08-09 Liverpool Home
1 46605 1 2019-08-09 Norwich City Away
2 46606 1 2019-08-10 AFC Bournemouth Home
AgainstTeam
0 Norwich City
1 Liverpool
2 Sheffield United
3 AFC Bournemouth
...
575 Sheffield United
576 Newcastle United
577 Southampton
这个片段使用了以下两种方法:
match_ids = []
home_away = []
dates = []
#For each row in the player matches dataframe...
for row in df_pm.itertuples():
#Look up the match id from the team matches dataframe
team = row.ForTeam
againstteam = row.AgainstTeam
gameweek = row.GameWeek
print (team,againstteam,gameweek)
match_id = df_melt.loc[(df_melt['GameWeek']==gameweek)
&(df_melt['Team']==team)
&(df_melt['AgainstTeam']==againstteam),
'MatchID'].item()
date = df_melt.loc[(df_melt['GameWeek']==gameweek)
&(df_melt['Team']==team)
&(df_melt['AgainstTeam']==againstteam),
'Date'].item()
home = df_melt.loc[(df_melt['GameWeek']==gameweek)
&(df_melt['Team']==team)
&(df_melt['AgainstTeam']==againstteam),
'Home'].item()
match_ids.append(match_id)
home_away.append(home)
dates.append(date)
在第一次迭代中,我打印:
Liverpool
Norwich City
1
但我得到了一个错误:
Traceback (most recent call last):
File "tableau_data_generation.py", line 166, in <module>
'MatchID'].item()
File "/Users/me/anaconda2/envs/data_science/lib/python3.7/site-packages/pandas/core/base.py", line 652, in item
return self.values.item()
ValueError: can only convert an array of size 1 to a Python scalar
如何解决此问题?当您对本应收到的系列使用item()时:
FutureWarning: `item` has been deprecated and will be removed in a future version
由于item()在0.25.0版中已被弃用,因此看起来您使用了
一些过时的熊猫版本,可能你应该从升级开始
即使在较新版本的Pandas中,也可以使用item(),但在Numpy上
数组(至少现在没有弃用)。
因此,请将代码更改为:
df_melt.loc[...].values.item()
df_melt.loc[...].iloc[0]
另一个选项是使用iloc[0],因此您还可以将代码更改为:
df_melt.loc[...].values.item()
df_melt.loc[...].iloc[0]
编辑
如果df_熔化,上述解决方案仍可能引发异常(索引器错误)
找不到任何符合给定条件的行
使您的代码抵抗这种情况(并返回一些默认值)
您可以添加一个获取给定属性的函数(attr,实际上是
列)从满足给定标准的第一行开始(游戏周、团队、,
和反对小组):
然后,代替所有3个…=df_melt.loc[…].item()
运行指令:
match_id = getAttr(gameweek, team, againstteam, 'MatchID', default=-1)
date = getAttr(gameweek, team, againstteam, 'Date')
home = getAttr(gameweek, team, againstteam, 'Home', default='????')
也许如果你的df_melt.loc没有找到任何行,那么.item()方法就会抛出这个错误。@Joãovitor sim cara,我用
shape
检查了这个错误。但是如果我打印这些行,它们会去哪里呢?我的熊猫版本是1.0.3
df_melt.loc[…].values.item()
会抛出相同的错误。而df_melt.loc[…].iloc[0]
抛出我索引器:单个位置索引器超出范围
这意味着.loc[…]没有返回任何内容。将此部分更改为您自己的函数,即:1。调用.loc[…],并将结果保存在变量中。2.检查此结果是否为空(大小==0)。3.在这种情况下返回一些“代理”值。