2

I want to make a line chart by this code :

df = pd.DataFrame.from_dict({ 'sentencess' : sentencess, 'publishedAts' : publishedAts, 'hasil_sentimens' : hasil_sentimens })
df.to_csv('chart.csv')

df['publishedAts'] = pd.to_datetime(df['publishedAts'], errors='coerce')

by_day_sentiment = df.groupby([pd.Grouper(key='publishedAts',freq='D'),'hasil_sentimens']).size().unstack('hasil_sentimens')

sentiment_dict = by_day_sentiment.to_dict('dict')

and the output from sentiment_dict is

{'Negatif ': {Timestamp('2019-08-26 00:00:00', freq='D'): 2.0, Timestamp('2019-08-27 00:00:00', freq='D'): 4.0, Timestamp('2019-08-28 00:00:00', freq='D'): 2.0, Timestamp('2019-08-29 00:00:00', freq='D'): 3.0}, 'Netral ': {Timestamp('2019-08-26 00:00:00', freq='D'): 1.0, Timestamp('2019-08-27 00:00:00', freq='D'): 3.0, Timestamp('2019-08-28 00:00:00', freq='D'): 1.0, Timestamp('2019-08-29 00:00:00', freq='D'): 3.0}, 'Positif ': {Timestamp('2019-08-26 00:00:00', freq='D'): nan, Timestamp('2019-08-27 00:00:00', freq='D'): nan, Timestamp('2019-08-28 00:00:00', freq='D'): nan, Timestamp('2019-08-29 00:00:00', freq='D'): 1.0}}

From that sentiment_dict, how to make a new dict but the key (which is now datetime) is changed to a string?

3 Answers 3

2

Use strftime('%Y-%m-%d %H:%M:%S')

Ex:

from pandas import Timestamp
from numpy import nan
data = {'Negatif ': {Timestamp('2019-08-26 00:00:00', freq='D'): 2.0, Timestamp('2019-08-27 00:00:00', freq='D'): 4.0, Timestamp('2019-08-28 00:00:00', freq='D'): 2.0, Timestamp('2019-08-29 00:00:00', freq='D'): 3.0}, 'Netral ': {Timestamp('2019-08-26 00:00:00', freq='D'): 1.0, Timestamp('2019-08-27 00:00:00', freq='D'): 3.0, Timestamp('2019-08-28 00:00:00', freq='D'): 1.0, Timestamp('2019-08-29 00:00:00', freq='D'): 3.0}, 'Positif ': {Timestamp('2019-08-26 00:00:00', freq='D'): nan, Timestamp('2019-08-27 00:00:00', freq='D'): nan, Timestamp('2019-08-28 00:00:00', freq='D'): nan, Timestamp('2019-08-29 00:00:00', freq='D'): 1.0}}

print({k: {m.strftime('%Y-%m-%d %H:%M:%S'): v for m, v in v.items()} for k, v in data.items()})

Output:

{'Negatif ': {'2019-08-26 00:00:00': 2.0,
              '2019-08-27 00:00:00': 4.0,
              '2019-08-28 00:00:00': 2.0,
              '2019-08-29 00:00:00': 3.0},
 'Netral ': {'2019-08-26 00:00:00': 1.0,
             '2019-08-27 00:00:00': 3.0,
             '2019-08-28 00:00:00': 1.0,
             '2019-08-29 00:00:00': 3.0},
 'Positif ': {'2019-08-26 00:00:00': nan,
              '2019-08-27 00:00:00': nan,
              '2019-08-28 00:00:00': nan,
              '2019-08-29 00:00:00': 1.0}}
Sign up to request clarification or add additional context in comments.

Comments

2

To convert a DateTime object to a string you can use DateTime.strftime(FORMAT_STRING):

import datetime

x = datetime.datetime.now()

print(x.strftime("%H:%M:%S")) 

You can try it here https://repl.it/repls/ImmaterialAlarmingGenre

You more information on FORMAT_STRING see: https://www.w3schools.com/python/python_datetime.asp

Comments

0

You can add this line before parsing datafrmae to dictionary:

by_day_sentiment = df.groupby([pd.Grouper(key='publishedAts',freq='D'),'hasil_sentimens']).size().unstack('hasil_sentimens')

by_day_sentiment['publishedAts'] = by_day_sentiment['publishedAts'].astype(object)

sentiment_dict = by_day_sentiment.to_dict('dict')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.