Pandas - Некоторые столбцы не работают должным образом после groupby () - PullRequest
0 голосов
/ 09 июля 2020

Я пытаюсь создать простой график, но после применения это не имеет для меня никакого смысла groupBy()

enter image description here

df_cumulate = df.groupby(['date','sentiment'], as_index=False).sum()

def plot_df(df, x, y, title="", xlabel='Date', ylabel='Sentiment', dpi=100):
    plt.figure(figsize=(16,5), dpi=dpi)
    plt.plot(x, y, color='tab:red')
    plt.gca().set(title=title, xlabel=xlabel, ylabel=ylabel)
    plt.savefig('sentiment_over_time.png')
    plt.show()

plot_df(df_cumulate, x=df_cumulate.index, y=df_cumulate.sentiment, title='Sentiment Over Time')

If I changed into x=df_cumulate.date, will get this. The Sentiment sum() logically must more than 1 or below than -1.

enter image description here

dataset: https://gist.github.com/datomnurdin/33961755b306bc67e4121052ae87cfbc

1 Ответ

0 голосов
/ 09 июля 2020

Думаю, вы просто хотите сгруппировать по дате. Это похоже на то, что вы ожидали?

df_cumulate = df.groupby(['date'], as_index=False).sum()
display(df_cumulate)

def plot_df(df, x, y, title="", xlabel='Date', ylabel='Sentiment', dpi=100):
    plt.figure(figsize=(16,5), dpi=dpi)
    plt.plot(x, y, color='tab:red')
    plt.gca().set(title=title, xlabel=xlabel, ylabel=ylabel)
    plt.savefig('sentiment_over_time.png')
    plt.show()

plot_df(df_cumulate, x=df_cumulate.date, y=df_cumulate.sentiment, title='Sentiment Over Time')

введите описание изображения здесь

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...