解決したいこと
pandasで以下のcsvファイルを取得し、折れ線グラフを作図したいのですが、データが新しい順番に並んでいるためグラフが新しい→古いの順番になってしまいます。そのため、データの列を反転したいです。
0 28631.45 1 28756.86 2 28523.26 3 28633.46 4 28242.21 ... 2490 10228.92 2491 10344.54 2492 10292.63 2493 10355.99 2494 10279.19
試したこと
調べると、data.iloc[::-1]を使えば反転できるとのことだったのですが、
2494 10279.19 2493 10355.99 2492 10292.63 2491 10344.54 2490 10228.92 ... 4 28242.21 3 28633.46 2 28523.26 1 28756.86 0 28631.45
このようになり、解決できず。。。
以下、ソースコードおよびcsvファイル
python
1import matplotlib.pyplot as plt 2import numpy as np 3import csv 4import pandas as pd 5df = pd.read_csv("stock.csv") 6#データを反転 7df = df.iloc[::-1] 8x = df['finish'] 9#カラムごとapply 10x = x.apply(lambda s: float(s.replace(',',''))) 11print(x) 12plt.figure() 13x.plot() 14plt.savefig('stock.png') 15plt.show() 16plt.close('all')
stock.csv
1"日付け","finish","始値","高値","安値","出来高","前日比%" 2"2021年01月22日","28,631.45","28,580.20","28,698.18","28,527.16","669.45M","-0.44%" 3"2021年01月21日","28,756.86","28,710.41","28,846.15","28,677.61","676.62M","0.82%" 4"2021年01月20日","28,523.26","28,798.74","28,801.19","28,402.11","697.92M","-0.38%" 5"2021年01月19日","28,633.46","28,405.49","28,720.91","28,373.34","581.76M","1.39%" 6"2021年01月18日","28,242.21","28,238.68","28,349.97","28,111.54","519.15M","-0.97%" 7"2021年01月15日","28,519.18","28,777.47","28,820.50","28,477.03","741.16M","-0.62%" 8"2021年01月14日","28,698.26","28,442.73","28,979.53","28,411.58","819.13M","0.85%" 9"2021年01月13日","28,456.59","28,140.10","28,503.43","28,133.59","708.57M","1.04%" 10"2021年01月12日","28,164.34","28,004.37","28,287.37","27,899.45","788.40M","0.09%" 11"2021年01月08日","28,139.03","27,720.14","28,139.03","27,667.75","848.72M","2.36%" 12"2021年01月07日","27,490.13","27,340.46","27,624.73","27,340.46","988.56M","1.60%" 13"2021年01月06日","27,055.94","27,102.85","27,196.40","27,002.18","726.61M","-0.38%" 14"2021年01月05日","27,158.63","27,151.38","27,279.78","27,073.46","550.01M","-0.37%" 15"2021年01月04日","27,258.38","27,575.57","27,602.11","27,042.32","515.06M","-0.68%" 16"2020年12月30日","27,444.17","27,559.10","27,572.57","27,338.56","505.87M","-0.45%" 17"2020年12月29日","27,568.15","26,936.38","27,602.52","26,921.14","594.33M","2.66%" 18"2020年12月28日","26,854.03","26,691.29","26,854.03","26,664.60","506.67M","0.74%" 19"2020年12月25日","26,656.61","26,708.10","26,716.61","26,638.28","333.99M","-0.04%" 20"2020年12月24日","26,668.35","26,635.11","26,764.53","26,605.26","479.33M","0.54%" 21"2020年12月23日","26,524.79","26,580.43","26,585.21","26,414.74","560.12M","0.33%"
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/01/26 13:32