英文:
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('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()
答案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)
后续
你提到的下一个问题是&
运算符在处理日期数据时出错。这可能出现在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'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 `&` operator complaining about date data. That's probably in `filterData`. The expression `df['weekday'].isin(["5", "6", "7"])` 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'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 `&`.
And again, I suspect you really want `.isin([5,6,7])` here, assuming that pandas read that column as integers, not strings.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论