英文:
graph monthly exchange rate
问题
I see the issue in your code. You're trying to filter the DataFrame using (usyen.Date == 1997, 1998)
which is causing the 'DataFrame' object is not callable error. To filter data for the years 1997 and 1998, you should use the .loc
method like this:
x = usyen.loc[(usyen['Date'] == 1997) | (usyen['Date'] == 1998)]['Date']
Replace the line with this code, and your graph should display correctly.
英文:
I took the data from this website https://www.federalreserve.gov/releases/H10/hist/dat96_ja.txt
I want to make a line graph showing the average monthly rate in 1997 and 1998.
So the x-axis will be Jan 97, Feb 97, ....., Dec 98.
This is what I have done until now
import pandas as pd
usyen = pd.read_csv("usyen.csv")
usyen = usyen.iloc[6:]
usyen[['Date','Rate']] = usyen.Column1.str.split(expand=True)
usyen.reset_index(inplace=True)
usyen = usyen.drop(['Column1', 'Column2', 'index'], axis=1)
usyen
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(style = 'whitegrid')
fig, ax = plt.subplots(figsize = (10,5))
x = usyen(usyen.Date == 1997, 1998)['Date']
y = usyen['Rate']
ax.plot(x,y)
ax.set_title('Yen/US Exchange Rate')
ax.set_xlabel('Year')
ax.set_ylabel('Rate')
My problem is that the graph doesn't show up.
Error: 'DataFrame' object is not callable
Thank you in advance
答案1
得分: 1
关于你的错误:
x = usyen(usyen.Date == 1997, 1998)['Date']
为了筛选你的数据框,你应该使用 [...]
而不是 (...)
. 修复你的代码可以这样写:
m = usyen['Date'].str.contains(r'-(97|98)$') # 以-97或-98结尾
x = usyen.loc[m, 'Date']
y = usyen.loc[m, 'Rate']
不过,你可以优化你的代码:
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(style='whitegrid')
# 读取数据
url = 'https://www.federalreserve.gov/releases/H10/hist/dat96_ja.txt'
hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/112.0'}
df = pd.read_csv(url, skiprows=6, header=None, na_values='ND', names=['Date', 'Rate'], sep='\s+', engine='python', storage_options=hdr)
df['Date'] = pd.to_datetime(df['Date'], format='%d-%b-%y')
# 提取子集并绘图
df1 = df[df['Date'].between('1997-01-01', '1998-12-31', inclusive='left')]
ax = df1.plot(x='Date', y='Rate', title='Yen/US Exchange Rate', legend=False)
plt.tight_layout()
plt.show()
输出:
英文:
About your error:
x = usyen(usyen.Date == 1997, 1998)['Date']
To filter your dataframe, you should use [...]
and not (...)
. To fix your code use:
m = usyen['Date'].str.contains(r'-(97|98)$') # ends with -97 or -98
x = usyen.loc[m, 'Date']
y = usyen.loc[m, 'Rate']
However, you can optimize your code:
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(style = 'whitegrid')
# Read data
url = 'https://www.federalreserve.gov/releases/H10/hist/dat96_ja.txt'
hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/112.0'}
df = pd.read_csv(url, skiprows=6, header=None, na_values='ND', names=['Date', 'Rate'], sep='\s+', engine='python', storage_options=hdr)
df['Date'] = pd.to_datetime(df['Date'], format='%d-%b-%y')
# Extract subset and plot
df1 = df[df['Date'].between('1997-01-01', '1998-12-31', inclusive='left')]
ax = df1.plot(x='Date', y='Rate', title='Yen/US Exchange Rate', legend=False)
plt.tight_layout()
plt.show()
Output:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论