asif 807
asif 807

Reputation: 21

Slicing from date from csv file rows

    date    LEV1    LEV2    LEV3    L2  L3  L4
2013-01-01 1:00 266.591 266.591 266.591 1000    1200    1400
2013-01-01 2:00 266.479 266.479 266.479 1000    1200    1400
2013-01-01 3:00 266.373 266.373 266.373 1000    1200    1400
2013-01-01 4:00 266.273 266.273 266.273 1000    1200    1400
2013-01-01 5:00 266.178 266.178 266.178 1000    1200    1400
2013-01-01 6:00 265.05  265.05  265.05  1000    1200    1400
2013-01-01 7:00 266.065 266.065 266.065 1000    1200    1400

I want to select and plot the data (from pandas dataframe) as X and Y for each hour on multiple figures for the selected range (say 2013-01-01 20:00 to 2013-01-02 15:00).

I want to select rows based on selected range for values from column 2~4 as X-axis with 3 records in each row [273.548 273.322 272.8313] and

for the same date range another set of rows as Y-axis e.g. [1000 1200 1400] for values from columns 39~41. I tried with

import pandas as pd

dft = pd.read_table("3year.csv", sep=" ",delimiter =",")
J = dft[['LEV1','LEV2', 'LEV3']].as_matrix()
G = dft[['L2','L3','L4']].as_matrix()
print J




 [ 270.     270.     270.]
 [ 270.     270.     270.]
 [ 270.     270.     270.]
 ..., 
 [ 273.548  273.322  272.831]
 [ 273.575  273.289  272.634]
 [ 273.598  273.254  272.43]]

but I can not select based on dates as it selects out all the data for the relevant columns

Upvotes: 2

Views: 160

Answers (1)

su79eu7k
su79eu7k

Reputation: 7316

There must be various(and smarter) way to achieve your goal. But I will show you one possible example using pandas datetimeIndex.

df['date'] = pd.to_datetime(df['date'])
df = df.set_index('date')

selector_LEV = (df['LEV1'] <= 266.373) & (df['LEV2'] <= 266.373) & (df['LEV3'] <= 266.373)
selector_L = (df['L2'] == 1000) & (df['L3'] == 1200) & (df['L4'] == 1400)

df[selector_LEV & selector_L]['20130101':]

    LEV1    LEV2    LEV3    L2  L3  L4
date                        
2013-01-01 03:00:00 266.373 266.373 266.373 1000    1200    1400
2013-01-01 04:00:00 266.273 266.273 266.273 1000    1200    1400
2013-01-01 05:00:00 266.178 266.178 266.178 1000    1200    1400
2013-01-01 06:00:00 265.050 265.050 265.050 1000    1200    1400
2013-01-01 07:00:00 266.065 266.065 266.065 1000    1200    1400

Now you can go further your way, or directly plot using the pandas plotting feature(Presumably it's based on matplotlib) like df[selector_LEV & selector_L]['20130101':].plot().

Upvotes: 1

Related Questions