我正在尝试分析Nba球员数据集以进行作业。我有一个包含名称,得分,助攻,年等的数据集。我试图分析2017年出战的球员,所以首先我要打印他们的姓名,得分,助攻等。
当我尝试以下代码时:
for i in df1.columns:
if df1['Year'] == 2017:
print(df1.loc[:, ["Year","Player","Pos","Team","Games","AST","PTS","PPG"]])
此代码出现错误,告诉我需要添加.any()
或.all()
。
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-101-c1ff6e025cb7> in <module>()
1 for i in df1.columns:
----> 2 if df1['Year'] == 2017:
3 print(df1.loc[:, ["Year","Player","Pos","Team","Games","AST","PTS","PPG"]])
/opt/conda/lib/python3.6/site-packages/pandas/core/generic.py in __nonzero__(self)
1574 raise ValueError("The truth value of a {0} is ambiguous. "
1575 "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
-> 1576 .format(self.__class__.__name__))
1577
1578 __bool__ = __nonzero__
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
我将它们添加到了代码中。
for i in df1.columns:
if df1['Year'].all() == 2017:
print(df1.loc[:, ["Year","Player","Pos","Team","Games","AST","PTS","PPG"]])
此代码块不返回任何输出(无错误等)
我还认为可以通过列表理解来做到这一点,但我找不到办法。应该是这样的:
df2 = [df1['Year','Player','Pos','Team','Games','AST','PTS','PPG'] if (df1['Year'].any() == 2017) else "Bye" for i in df1.Year]
print(df2)
但是这里有一个问题,就是这段代码总是输入else语句,并且只显示“ Bye”。
答案 0 :(得分:0)
如果您只想打印年份为2017的其他列,则可以这样做
print(df1[df1['Year']==2017].loc[:,["Year","Player","Pos","Team","Games","AST","PTS","PPG"]])
无需像您那样遍历列或使用if语句,pandas数据框就具有良好的索引