月度汇率图

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

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:

  1. 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

  1. import pandas as pd
  2. usyen = pd.read_csv("usyen.csv")
  3. usyen = usyen.iloc[6:]
  4. usyen[['Date','Rate']] = usyen.Column1.str.split(expand=True)
  5. usyen.reset_index(inplace=True)
  6. usyen = usyen.drop(['Column1', 'Column2', 'index'], axis=1)
  7. usyen
  8. import matplotlib.pyplot as plt
  9. import seaborn as sns
  10. sns.set(style = 'whitegrid')
  11. fig, ax = plt.subplots(figsize = (10,5))
  12. x = usyen(usyen.Date == 1997, 1998)['Date']
  13. y = usyen['Rate']
  14. ax.plot(x,y)
  15. ax.set_title('Yen/US Exchange Rate')
  16. ax.set_xlabel('Year')
  17. 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

关于你的错误:

  1. x = usyen(usyen.Date == 1997, 1998)['Date']

为了筛选你的数据框,你应该使用 [...] 而不是 (...). 修复你的代码可以这样写:

  1. m = usyen['Date'].str.contains(r'-(97|98)$') # 以-97或-98结尾
  2. x = usyen.loc[m, 'Date']
  3. y = usyen.loc[m, 'Rate']

不过,你可以优化你的代码:

  1. import pandas as pd
  2. import matplotlib.pyplot as plt
  3. import seaborn as sns
  4. sns.set(style='whitegrid')
  5. # 读取数据
  6. url = 'https://www.federalreserve.gov/releases/H10/hist/dat96_ja.txt'
  7. hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/112.0'}
  8. df = pd.read_csv(url, skiprows=6, header=None, na_values='ND', names=['Date', 'Rate'], sep='\s+', engine='python', storage_options=hdr)
  9. df['Date'] = pd.to_datetime(df['Date'], format='%d-%b-%y')
  10. # 提取子集并绘图
  11. df1 = df[df['Date'].between('1997-01-01', '1998-12-31', inclusive='left')]
  12. ax = df1.plot(x='Date', y='Rate', title='Yen/US Exchange Rate', legend=False)
  13. plt.tight_layout()
  14. plt.show()

输出:

月度汇率图

英文:

About your error:

  1. x = usyen(usyen.Date == 1997, 1998)['Date']

To filter your dataframe, you should use [...] and not (...). To fix your code use:

  1. m = usyen['Date'].str.contains(r'-(97|98)$') # ends with -97 or -98
  2. x = usyen.loc[m, 'Date']
  3. y = usyen.loc[m, 'Rate']

However, you can optimize your code:

  1. import pandas as pd
  2. import matplotlib.pyplot as plt
  3. import seaborn as sns
  4. sns.set(style = 'whitegrid')
  5. # Read data
  6. url = 'https://www.federalreserve.gov/releases/H10/hist/dat96_ja.txt'
  7. hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/112.0'}
  8. df = pd.read_csv(url, skiprows=6, header=None, na_values='ND', names=['Date', 'Rate'], sep='\s+', engine='python', storage_options=hdr)
  9. df['Date'] = pd.to_datetime(df['Date'], format='%d-%b-%y')
  10. # Extract subset and plot
  11. df1 = df[df['Date'].between('1997-01-01', '1998-12-31', inclusive='left')]
  12. ax = df1.plot(x='Date', y='Rate', title='Yen/US Exchange Rate', legend=False)
  13. plt.tight_layout()
  14. plt.show()

Output:

月度汇率图

huangapple
  • 本文由 发表于 2023年5月7日 11:59:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76192143.html
匿名

发表评论

匿名网友

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

确定