简体   繁体   中英

How to select a specific date range in a csv file with pandas?

I am new to Python playing around with a csv file. I would like to find a way to print out my graph by selecting a specific date range, for example 2013-03-20:2014-03-04.

Code below:

import pandas as pd
import matplotlib.pyplot as plt

prc=pd.read_csv("csv",parse_dates=True, nrows=150, usecols=["Close"])

prc_ma=prc.rolling(5).mean()


plt.plot(prc, color="blue", label="Price")
plt.plot(prc_ma, color="red", label="Moving Average")
plt.xlabel("Date")
plt.ylabel("Price")
plt.title("Moving Average")
plt.grid()

I currently work with the parameter nrows.

Thank you

Simply filter for the dates with .loc assuming datetimes are the index of dataframe:

prc = pd.read_csv("csv", parse_dates=True, nrows=150, usecols=["Close"])

prc_sub = prc.loc['2013-03-20':'2014-03-04']

To demonstrate with random data subsetted out of all days of 2013 and 2014:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

pd.set_option('display.width', 1000)
np.random.seed(1001)

prc = pd.DataFrame({'PRICE': abs(np.random.randn(730))}, 
                    index=pd.date_range("2013-01-01", "2014-12-31", freq="D"))

# SUBSETTED DATAFRAME
prc_sub = prc.loc['2013-03-20':'2014-03-04']

prc_ma = prc_sub.rolling(5).mean()

plt.plot(prc_sub, color="blue", label="Price")
plt.plot(prc_ma, color="red", label="Moving Average")
plt.xlabel("Date")
plt.ylabel("Price")
plt.title("Moving Average")
plt.grid()

绘图输出

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM