无法将数据框绘制为barh,因为TypeError:空'DataFrame':没有要绘制的数字数据

时间:2018-11-26 04:51:37

标签: python-3.x pandas matplotlib

我一直在这个网站和Google上解决这个问题。
似乎在制作可打印数据框时缺少基本概念。
我尝试确保在“团队”中有一列字符串,在“积分”中有一列整数
我仍然得到:TypeError:空的'DataFrame':没有要绘制的数字数据

import csv
import pandas
import numpy
import matplotlib.pyplot as plt
from matplotlib.ticker import StrMethodFormatter

set_of_teams = set()

def load_epl_games(file_name):
    with open(file_name, newline='') as csvfile:
        reader = csv.DictReader(csvfile)
        raw_data = {"HomeTeam": [], "AwayTeam": [], "FTHG": [], "FTAG": [], "FTR": []}
        for row in reader:
            set_of_teams.add(row["HomeTeam"])
            set_of_teams.add(row["AwayTeam"])
            raw_data["HomeTeam"].append(row["HomeTeam"])
            raw_data["AwayTeam"].append(row["AwayTeam"])
            raw_data["FTHG"].append(row["FTHG"])
            raw_data["FTAG"].append(row["FTAG"])
            raw_data["FTR"].append(row["FTR"])
        data_frame = pandas.DataFrame(data=raw_data)
    return data_frame

def calc_points(team, table):
    points = 0
    for row_number in range(table["HomeTeam"].count()):
        home_team = table.loc[row_number, "HomeTeam"]
        away_team = table.loc[row_number, "AwayTeam"]
        if team in [home_team, away_team]:
            home_team_points = 0
            away_team_points = 0
            winner = table.loc[row_number, "FTR"]
            if winner == 'H':
                home_team_points = 3
            elif winner == 'A':
                away_team_points = 3
            else:
                home_team_points = 1
                away_team_points = 1
            if team == home_team:
                points += home_team_points
            else:
                points += away_team_points
    return points

def get_goals_scored_conceded(team, table):
    scored = 0
    conceded = 0
    for row_number in range(table["HomeTeam"].count()):
        home_team = table.loc[row_number, "HomeTeam"]
        away_team = table.loc[row_number, "AwayTeam"]
        if team in [home_team, away_team]:
            if team == home_team:
                scored += int(table.loc[row_number, "FTHG"])
                conceded += int(table.loc[row_number, "FTAG"])
            else:
                scored += int(table.loc[row_number, "FTAG"])
                conceded += int(table.loc[row_number, "FTHG"])
    return (scored, conceded)

def compute_table(df):
    raw_data = {"Team": [], "Points": [], "GoalDifference":[], "Goals": []}
    for team in set_of_teams:
        goal_data = get_goals_scored_conceded(team, df)
        raw_data["Team"].append(team)
        raw_data["Points"].append(calc_points(team, df))
        raw_data["GoalDifference"].append(goal_data[0] - goal_data[1])
        raw_data["Goals"].append(goal_data[0])
    data_frame = pandas.DataFrame(data=raw_data)
    data_frame = data_frame.sort_values(["Points", "GoalDifference", "Goals"], ascending=[False, False, False]).reset_index(drop=True)
    data_frame.index = numpy.arange(1,len(data_frame)+1)
    data_frame.index.names = ["Finish"]
    return data_frame

def get_finish(team, table):
    return table[table.Team==team].index.item()

def get_points(team, table):
    return table[table.Team==team].Points.item()

def display_hbar(tables):
    raw_data = {"Team": [], "Points": []}
    for row_number in range(tables["Team"].count()):
        raw_data["Team"].append(tables.loc[row_number+1, "Team"])
        raw_data["Points"].append(int(tables.loc[row_number+1, "Points"]))
    df = pandas.DataFrame(data=raw_data)
    #df = pandas.DataFrame(tables, columns=["Team", "Points"])
    print(df)
    print(df.dtypes)
    df["Points"].apply(int)
    print(df.dtypes)
    df.plot(kind='barh',x='Points',y='Team')

games = load_epl_games('epl2016.csv')
final_table = compute_table(games)
#print(final_table)
#print(get_finish("Tottenham", final_table))
#print(get_points("West Ham", final_table))
display_hbar(final_table)


输出:

              Team  Points
0          Chelsea      93
1        Tottenham      86
2         Man City      78
3        Liverpool      76
4          Arsenal      75
5       Man United      69
6          Everton      61
7      Southampton      46
8      Bournemouth      46
9        West Brom      45
10        West Ham      45
11       Leicester      44
12           Stoke      44
13  Crystal Palace      41
14         Swansea      41
15         Burnley      40
16         Watford      40
17            Hull      34
18   Middlesbrough      28
19      Sunderland      24
Team      object
Points     int64
dtype: object
Team      object
Points     int64
dtype: object
Traceback (most recent call last):
  File "C:/Users/Michael/Documents/Programming/Python/Premier League.py", line 99, in <module>
    display_hbar(final_table)
  File "C:/Users/Michael/Documents/Programming/Python/Premier League.py", line 92, in display_hbar
    df.plot(kind='barh',x='Points',y='Team')
  File "C:\Program Files (x86)\Python36-32\lib\site-    packages\pandas\plotting\_core.py", line 2941, in __call__
    sort_columns=sort_columns, **kwds)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\pandas\plotting\_core.py", line 1977, in plot_frame
**kwds)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\pandas\plotting\_core.py", line 1804, in _plot
    plot_obj.generate()
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\pandas\plotting\_core.py", line 258, in generate
    self._compute_plot_data()
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\pandas\plotting\_core.py", line 373, in _compute_plot_data
'plot'.format(numeric_data.__class__.__name__))
TypeError: Empty 'DataFrame': no numeric data to plot


我在display_hbar函数中出了什么问题,导致我无法绘制数据?

这里是csv file

2 个答案:

答案 0 :(得分:2)

df.plot(x = "Team", y="Points", kind="barh");

enter image description here

答案 1 :(得分:1)

您应在x中交换ydf.plot(...)。因为根据熊猫documentationy必须是数字。