我运行我的代码时,在Python中没有得到我想要的直方图。

huangapple go评论70阅读模式
英文:

When I run my code I don't get the histograms that I want in Python

问题

我不知道我做错了什么,但当我运行我的代码时,我看不到任何东西。这是代码:

import pandas as pd
import matplotlib.pyplot as plt

def readData():
    df = pd.read_excel('BakeryData_Vilnius.xlsx')
    print(df.head())
    return df

def filterData(df):
    filterWeekday = df[df['weekday'].isin(["5", "6", "7"])]
    filterDate = df[(df['date'] < '2021-03-01') & (df['date'] > '2022-03-01')]
    dataFiltered = df.loc[filterWeekday & filterDate]
    return dataFiltered 

def dataMainStreetA(dataFiltered):
    d_MSA_5 = dataFiltered[(dataFiltered['weekday']== 5) & (dataFiltered['main street A'])]
    d_MSA_6 = dataFiltered[(dataFiltered['weekday']== 6) & (dataFiltered['main street A'])]
    d_MSA_7 = dataFiltered[(dataFiltered['weekday']== 7) & (dataFiltered['main street A'])]
    return d_MSA_5, d_MSA_6, d_MSA_7

def dataStationA(dataFiltered):
    d_SA_5 = dataFiltered[(dataFiltered['weekday']== 5) & (dataFiltered['station A'])]
    d_SA_6 = dataFiltered[(dataFiltered['weekday']== 6) & (dataFiltered['station A'])]
    d_SA_7 = dataFiltered[(dataFiltered['weekday']== 7) & (dataFiltered['station A'])]
    return d_SA_5, d_SA_6, d_SA_7

def plotHistograms (d_MSA_5, d_MSA_6, d_MSA_7, d_SA_5, d_SA_6, d_SA_7):
    plt.figure(figsize=(10, 6))

    plt.subplot(2,3,1) 
    plt.hist(d_MSA_5, bins=15)
    plt.title('Main Street A - Friday')
    plt.xlabel('Demand')
    plt.ylabel('Frequency')

    plt.subplot(2,3,2)
    plt.hist(d_MSA_6, bins=15)
    plt.title('Main Street A - Saturday')
    plt.xlabel('Demand')
    plt.ylabel('Frequency')

    plt.subplot(2,3,3)
    plt.hist(d_MSA_7, bins=15)
    plt.title('Main Street A - Saturday')
    plt.xlabel('Demand')
    plt.ylabel('Frequency')

    plt.subplot(2,3,4)
    plt.hist(d_SA_5, bins=15)
    plt.title('Station A - Friday')
    plt.xlabel('Demand')
    plt.ylabel('Frequency')

    plt.subplot(2,3,5)
    plt.hist(d_SA_6, bins=15)
    plt.title('Station A - Saturday')
    plt.xlabel('Demand')
    plt.ylabel('Frequency')

    plt.subplot(2,3,6)
    plt.hist(d_SA_7, bins=15)
    plt.title('Station A - Sunday')
    plt.xlabel('Demand')
    plt.ylabel('Frequency')

    plt.tight_layout()
    plt.show()
英文:

I don't know what I'm doing wrong, but I don't see anything when I run my code. This is the code:

import pandas as pd
import matplotlib.pyplot as plt
def readData():
df = pd.read_excel(&#39;BakeryData_Vilnius.xlsx&#39;)
print(df.head())
return df
def filterData(df):
filterWeekday = df[df[&#39;weekday&#39;].isin([&quot;5&quot;, &quot;6&quot;, &quot;7&quot;])]
filterDate = df[(df[&#39;date&#39;] &lt; &#39;2021-03-01&#39;) &amp; (df[&#39;date&#39;] &gt; &#39;2022-03-01&#39;)]
dataFiltered = df.loc[filterWeekday &amp; filterDate]
return dataFiltered 
def dataMainStreetA(dataFiltered):
d_MSA_5 = dataFiltered[(dataFiltered[&#39;weekday&#39;]== 5) &amp; (dataFiltered[&#39;main street A&#39;])]
d_MSA_6 = dataFiltered[(dataFiltered[&#39;weekday&#39;]== 6) &amp; (dataFiltered[&#39;main street A&#39;])]
d_MSA_7 = dataFiltered[(dataFiltered[&#39;weekday&#39;]== 7) &amp; (dataFiltered[&#39;main street A&#39;])]
return d_MSA_5, d_MSA_6, d_MSA_7
def dataStationA(dataFiltered):
d_SA_5 = dataFiltered[(dataFiltered[&#39;weekday&#39;]== 5) &amp; (dataFiltered[&#39;station A&#39;])]
d_SA_6 = dataFiltered[(dataFiltered[&#39;weekday&#39;]== 6) &amp; (dataFiltered[&#39;station A&#39;])]
d_SA_7 = dataFiltered[(dataFiltered[&#39;weekday&#39;]== 7) &amp; (dataFiltered[&#39;station A&#39;])]
return d_SA_5, d_SA_6, d_SA_7
def plotHistograms (d_MSA_5, d_MSA_6, d_MSA_7, d_SA_5, d_SA_6, d_SA_7):
plt.figure(figsize=(10, 6))
plt.subplot(2,3,1) 
plt.hist(d_MSA_5, bins=15)
plt.title(&#39;Main Street A - Friday&#39;)
plt.xlabel(&#39;Demand&#39;)
plt.ylabel(&#39;Frequency&#39;)
plt.subplot(2,3,2)
plt.hist(d_MSA_6, bins=15)
plt.title(&#39;Main Street A - Saturday&#39;)
plt.xlabel(&#39;Demand&#39;)
plt.ylabel(&#39;Frequency&#39;)
plt.subplot(2,3,3)
plt.hist(d_MSA_7, bins=15)
plt.title(&#39;Main Street A - Saturday&#39;)
plt.xlabel(&#39;Demand&#39;)
plt.ylabel(&#39;Frequency&#39;)
plt.subplot(2,3,4)
plt.hist(d_SA_5, bins=15)
plt.title(&#39;Station A - Friday&#39;)
plt.xlabel(&#39;Demand&#39;)
plt.ylabel(&#39;Frequency&#39;)
plt.subplot(2,3,5)
plt.hist(d_SA_6, bins=15)
plt.title(&#39;Station A - Saturday&#39;)
plt.xlabel(&#39;Demand&#39;)
plt.ylabel(&#39;Frequency&#39;)
plt.subplot(2,3,6)
plt.hist(d_SA_7, bins=15)
plt.title(&#39;Station A - Sunday&#39;)
plt.xlabel(&#39;Demand&#39;)
plt.ylabel(&#39;Frequency&#39;)
plt.tight_layout()
plt.show()

答案1

得分: 5

你还没有调用任何函数我怀疑你想在最后加上这个
```python
if __name__ == "__main__":
    df = readData()
    df = filterData(df)
    m5,m6,m7 = dataMainStreetA(df)
    d5,d6,d7 = dataStationA(df)
    plotHistograms(m5,m6,m7,d5,d6,d7)

后续

你提到的下一个问题是&amp;运算符在处理日期数据时出错。这可能出现在filterData函数中。表达式df['weekday'].isin(["5", "6", "7"])会产生一个布尔数组。当你将其传递给df[...]时,这会选择一系列行。因此,filterWeekday包含一系列行,而不是行选择器。因此,你的df.loc调用似乎有问题。

我认为你想要的是这样的,但是没有看到数据,我们不能确定:

def filterData(df):
    filterWeekday = df['weekday'].isin(["5", "6", "7"])
    filterDate = (df['date'] < '2021-03-01') & (df['date'] > '2022-03-01')
    return df[filterWeekday & filterDate]

在这种情况下,这两个变量都是布尔行选择数组,可以使用&组合。

我再次怀疑你实际上想要在这里使用.isin([5,6,7]),假设pandas将该列识别为整数而不是字符串。


<details>
<summary>英文:</summary>
You haven&#39;t CALLED any of your functions.  I suspect you want this at the end:

if name == "main":
df = readData()
df = filterData(df)
m5,m6,m7 = dataMainStreetA(df)
d5,d6,d7 = dataStationA(df)
plotHistograms(m5,m6,m7,d5,d6,d7)

## Followup
The next problem you mention is the `&amp;` operator complaining about date data.  That&#39;s probably in `filterData`.  The expression `df[&#39;weekday&#39;].isin([&quot;5&quot;, &quot;6&quot;, &quot;7&quot;])` produces an array of booleans.  When you pass that to `df[...]`, that selects a series of rows.  So, `filterWeekday` contains a series of rows, not row selectors.  Thus, your `df.loc` call seems wrong.
I THINK you want this, but without seeing the data, we can&#39;t be sure:

def filterData(df):
filterWeekday = df['weekday'].isin(["5", "6", "7"])
filterDate = (df['date'] < '2021-03-01') & (df['date'] > '2022-03-01')
return df[filterWeekday & filterDate]

In this case, both of the variables are boolean row selection arrays, which CAN be combined with `&amp;`.
And again, I suspect you really want `.isin([5,6,7])` here, assuming that pandas read that column as integers, not strings.
</details>

huangapple
  • 本文由 发表于 2023年6月26日 06:56:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76552707.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定