Reputation: 9
Here is my csv file in case you need it
I am trying to run through every MLB team's playoff appearances and record them, however "DataFrame" has not attribute to "str"
keeps popping up. Here is my code:
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)
Here is my error message:
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'
Upvotes: 0
Views: 1849
Reputation: 6486
You have to acquire the corresponding column first before trying to get access to the value of that cell. That line causing the issue should be changed to:
if df_plyoff[0].str.contains(str(i)):
I am assuming that the column containing the years has no assigned name so defaulted to 0
. Let me know if it does.
Upvotes: 1