Python DataFrame没有与str问题相关的属性

Python DataFrame没有与str问题相关的属性,python,csv,dataframe,Python,Csv,Dataframe,我试着浏览每一支MLB球队的季后赛出场情况并记录下来,但是“数据帧”并没有属性为“str”不断弹出。这是我的密码: record_dict = {} file=open("playoff_teams.csv", mode="r") for line in file.readlines(): split_line = line.split() record_dict[split_line[0]]=split_line[1:] df_playoffs = pd.DataFrame.f

我试着浏览每一支MLB球队的季后赛出场情况并记录下来,但是
“数据帧”并没有属性为“str”
不断弹出。这是我的密码:

record_dict = {}
file=open("playoff_teams.csv", mode="r")
for line in file.readlines():
    split_line = line.split()
    record_dict[split_line[0]]=split_line[1:]
df_playoffs = pd.DataFrame.from_dict(record_dict, orient='index' )

list(sorted(set(df_pitching.team))) == list(sorted(set(df_batting.team))) == list(sorted(set(df_playoffs.index)))

df_all = df_pitching.merge(df_batting, on=['year','team'], 
how='left', suffixes=('_pitching', '_batting'))

playoff_appearance = []
for i in range(1969, 2019):                                                                      
    df = df_all[df_all.year == i]

    for team in df.team:
        df_plyoff = df_playoffs[df_playoffs.index == team]

        if df_plyoff.str.contains(str(i))[0]:
            playoff_appearance.append(1)   
        else:
            playoff_appearance.append(0)

world_series = []
for i in range(1969, 2019):
    df = df_all[df_all.year == i]

    for team in df.team:
        df_ws = df_playoffs[df_playoffs.index == team]

        if df_ws.world_series.str.contains(str(i))[0]:  
            world_series.append(1)  
        else:
            world_series.append(0)
以下是我的错误消息:

Traceback (most recent call last):
  File "/Users/hannahbeegle/Desktop/Baseball.py", line 148, in <module>
    if df_plyoff.str.contains(str(i))[0]:
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/generic.py", line 5067, in __getattr__
    return object.__getattribute__(self, name)
AttributeError: 'DataFrame' object has no attribute 'str'
回溯(最近一次呼叫最后一次):
文件“/Users/hannahbeegle/Desktop/barball.py”,第148行,在
如果df_plyoff.str.包含(str(i))[0]:
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site packages/pandas/core/generic.py”,第5067行,位于__
返回对象。\uuuGetAttribute(self,name)
AttributeError:“DataFrame”对象没有属性“str”

在尝试访问该单元格的值之前,必须先获取相应的列。导致问题的行应更改为:

if df_plyoff[0].str.contains(str(i)):

我假设包含年份的列没有指定名称,因此默认为
0
。让我知道它是否存在。

如果df_plyoff.str.contains(str(i))[0]则您的错误行
失败,因为
df_plyoff
是一个
df
,您正试图过滤什么?如果df_plyoff['yeayy'].str.contains(str(i))[0]:
您是否希望
此外,应不惜一切代价避免循环,这是无性能的。您真正想在这里实现什么?错误告诉您问题的一半:数据帧没有
.str
属性。序列对象具有
.str
属性。你可以用这种方式对一列中的所有字符串进行操作,但不能对整个数据帧进行操作。@EdChum我试图在1970-2018年间循环,跟踪哪些球队进入了季后赛,哪些球队没有进入季后赛。这是我在将其改为:ValueError:一个系列的真值不明确时得到的错误。使用a.empty、a.bool()、a.item()、a.any()或a.all()。@Hannah您能显示
df_plyoff[0]
?Series([],Name:0,dtype:object)的输出吗?@Hannah好的,包含年份的列标签是什么?这才是重要的。显示
df_plyoff.columns
then.RangeIndex的输出(开始=0,停止=24,步骤=1)