如何使用尽可能少的代码在Jupyter笔记本中使用Python创建给定数据的频率分布表?

时间:2017-01-09 15:46:00

标签: python pandas statistics jupyter-notebook

开发一个汇总这些数据的频率分布。这些数据是在20天内对物体的需求。

2 1 0 2 1 3 0 2 4 0 3 2 3 4 2 2 2 4 3 0.任务是在jupyter笔记本中创建一个包含Demand和Frequency列的表。注意:需求必须按升序排列。这就是我所做的。

list_of_days = [2, 1, 0, 2, 1, 3, 0, 2, 4, 0, 3, 2 ,3, 4, 2, 2, 2, 4, 3, 0] # created a list of the data
import pandas as pd
series_of_days = pd.Series(list_of_days) # converted the list to series
series_of_days.value_counts(ascending = True) # the frequency was ascending but not the demand
test = dict(series_of_days.value_counts())
freq_table =  pd.Series(test)
pd.DataFrame({"Demand":freq_table.index, "Frequency":freq_table.values})

输出必须如下:

<table border = "1">

  <tr>
    <td>Demand</td>
    <td>Frequency</td>
  </tr>
  <tr>
    <td>0</td>
    <td>4</td>
  </tr>
  <tr>
    <td>1</td>
    <td>2</td>
  </tr>
  <tr>
    <td>2</td>
    <td>7</td>
  </tr>
<table>

等等。有没有更好的方法来缩短Python代码?或者提高效率?

4 个答案:

答案 0 :(得分:3)

您可以value_counts使用reset_index并按sort_values排序:

df1 = pd.Series(list_of_days).value_counts()
        .reset_index()
        .sort_values('index')
        .reset_index(drop=True)
df1.columns = ['Demand', 'Frequency']
print (df1)
   Demand  Frequency
0       0          4
1       1          2
2       2          7
3       3          4
4       4          3

sort_index排序的另一个类似解决方案:

df1 = pd.Series(list_of_days)
        .value_counts()
        .sort_index()
        .reset_index()
        .reset_index(drop=True)
df1.columns = ['Demand', 'Frequency']
print (df1)
   Demand  Frequency
0       0          4
1       1          2
2       2          7
3       3          4
4       4          3

答案 1 :(得分:1)

import collections
collections.Counter(list_of_days)

应该做你正在描述的事情

答案 2 :(得分:1)

我要为您发布的HTML表格的文字创建

pd.value_counts([2,1,0,2,1,3,0,2,4,0,3,2,3,4,2,2,2,4,3,0]).to_frame(name='Frequency').rename_axis('Demand', 1).sort_index()

enter image description here

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th>Demand</th>
      <th>Frequency</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>4</td>
    </tr>
    <tr>
      <th>1</th>
      <td>2</td>
    </tr>
    <tr>
      <th>2</th>
      <td>7</td>
    </tr>
    <tr>
      <th>3</th>
      <td>4</td>
    </tr>
    <tr>
      <th>4</th>
      <td>3</td>
    </tr>
  </tbody>
</table>

答案 3 :(得分:0)

如果你想要最短的,可能是这个代码,默认情况下,Counter会按升序对键进行排序。

},
"transformResponse": {

},
"timeout": 0,
"xsrfCookieName": "XSRF-TOKEN",
"xsrfHeaderName": "X-XSRF-TOKEN",
"maxContentLength": -1,
"headers": {
  "Accept": "application/json, text/plain, */*",
  "User-Agent": "axios/0.15.3",
  "host": "jsonplaceholder.typicode.com"
},
"method": "get",
"proxy": {
  "host": "http://proxy.com",
  "port": 8080
},
"url": "https://jsonplaceholder.typicode.com/posts"
  

[[0,4],[1,2],[2,7],[3,4],[4,3]]