Issue
I'm trying to build a graph from wikipedia by years (this is the ratio of wins and losses for Dynamo-Kyiv). When plotting the graph on the x-axis the years are not displayed. I don't understand why. In the dataframe the years are shown as an index. Here is the code on python for jupiter noutbook
from ipywidgets import interact
import ipywidgets as widgets
import pandas as pd
import requests
import matplotlib.pyplot as plt
import numpy as np
url = "https://ru.wikipedia.org/wiki/%D0%A1%D0%BF%D0%B8%D1%81%D0%BE%D0%BA_%D1%81%D0%B5%D0%B7%D0%BE%D0%BD%D0%BE%D0%B2_%D0%A4%D0%9A_%C2%AB%D0%94%D0%B8%D0%BD%D0%B0%D0%BC%D0%BE%C2%BB_%D0%9A%D0%B8%D0%B5%D0%B2"
response = requests.get(url)
dfs = pd.read_html(response.text)
df = dfs[0]
mask = pd.to_numeric(df['В'], errors='coerce').notna() & (pd.to_numeric(df['П'], errors='coerce').notna())
df = df[mask]
new_names = {
'Сезон': 'Year',
}
df = df.rename(columns=new_names)
df['Year'] = df['Year'].str.replace(r'\(.*\)', '', regex=True).str.replace(' ', '')
df['В'] = pd.to_numeric(df['В'], errors='coerce')
df['П'] = pd.to_numeric(df['П'], errors='coerce')
df['Year'] = pd.to_numeric(df['Year'], errors='coerce')
df['Ratio'] = df['В'] / (df['В']+df['П'])
df['TotalGames'] = df['В']+df['П']
df = df[['Year','Ratio', 'TotalGames']]
window_size = 5
degree = 4
coefficients = np.polyfit(df['Year'], df['Ratio'], degree)
df['PolynomialMovingAverage'] = np.polyval(coefficients, df['Year'])
df.set_index("Year")
potential_columns=[]
for col in df.columns:
potential_columns.append(col)
potential_columns.pop(0)
@interact(col_names=widgets.SelectMultiple(
options=potential_columns,
value=(potential_columns[0],),
description='Columns'))
def plot_data(col_names):
plt.figure(figsize=[12,8])
plt.gcf().suptitle(str(col_names))
if len(col_names) ==2:
plt.plot(df[list(col_names)[0]],'b')
ax=plt.twinx()
ax.plot(df[list(col_names)[1]],'r')
else:
plt.plot(df[list(col_names)])
Solution
There is a tiny mistake, you set the index but you forgot to assign the result to the dataframe, which doesn't happen automatically. Without an explicit assignment (option 1) you will need to use the inplace
flag (option 2):
change
df.set_index("Year")
to this
# option 1
df = df.set_index("Year")
# option 2
df.set_index("Year", inplace=True)
Answered By - Klops
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.