简体   繁体   中英

How to cnvert the date freq data into a dataframe in python and how to draw the histogram for that converted dataframe using matplotlib?

So i have a data like the below when i use this code:

>>datefreq = textdata[['date','smstext']]  
>>date_freq = datefreq.groupby('date').agg(len)
>>date_freq
                smstext_freq
   date
   2015-02-03    1
   2015-02-04    1500
   2015-02-05    13526
   2015-02-03    54444  

How can i convert this into a dataframe

expected output:

date_freq

      date          smstext_freq
  0  2015-02-03      1
  1  2015-02-04      1500
  2  2015-02-05      13526
  3  2015-02-03      54444  

please help me how to write the code for the above in python and please tell me how to draw the histogram for the converted data if you can

It appears that date_freq is already a Pandas DataFrame object. However, the dates are set as the index; you would like the dates to appear as an ordinary column. There is a one-line solution to this: use the reset_index() method.

>>> date_freq.reset_index(inplace=True)
>>> print data_freq

        date  smstext_freq
0 2015-02-03             1
1 2015-02-04          1500
2 2015-02-05         13526
3 2015-02-06         54444

Now you also want a histogram . Call the Pandas plot() method on date_freq['smstext_freq'] , and set kind='hist' . This will return a matplotlib.axes object, which you can fine-tune however you want. Here, I've just added a title and an x-axis label.

>>> ax = date_freq['smstext_freq'].plot(kind='hist')
>>> ax.set_title('SMS Text Frequency per Day')
>>> ax.set_xlabel('SMS Text Frequency')
>>> plt.show()

Here is the resulting histogram. It should look more interesting with more data, as well as with any other tweaks you may want to make to the graph. 短信直方图

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM