1

Here is a head() of my DataFrame df:

                     Temperature  DewPoint  Pressure
Date                                                
2010-01-01 00:00:00         46.2      37.5       1.0
2010-01-01 01:00:00         44.6      37.1       1.0
2010-01-01 02:00:00         44.1      36.9       1.0
2010-01-01 03:00:00         43.8      36.9       1.0
2010-01-01 04:00:00         43.5      36.8       1.0

I want to select from August 1 to August 15 2010 and display only the Temperature column.

What I am trying to do is: df.loc[['2010-08-01','2010-08-15'],'Temperature']

But this is throwing me an error.

Generally speaking what I want to learn is how, using loc method I can easily take a range of row i to row k and column j to p and show it in dataframe using loc method:

df.loc[[i:k],[j:p]]

Thank you very much in advance!!! Steve

9
  • 2
    does df['Temperature'].loc['2010-08-01':'2010-08-15'] work? Commented Feb 2, 2017 at 21:48
  • EdChum, yes it works, but why can't I do the same with loc[[i:k],[j:p]]? Why I need to "externally" define on what column I work? What if I want 2 columns? Commented Feb 2, 2017 at 21:51
  • For example this won't work: df['Temperature','DewPoint'].loc['2010-08-01':'2010-08-15'] Commented Feb 2, 2017 at 21:52
  • 1
    The issue here is that slice ranges with partial date string matching may not work here, it's the datetime that is the issue Commented Feb 2, 2017 at 21:52
  • 1
    That's a syntax error should be df[['Temperature','DewPoint']].loc['2010-08-01':'2010-08-15'] you need additional square brackets, it would evern without the loc Commented Feb 2, 2017 at 21:53

1 Answer 1

2

I think if you want to be able to pass a slice for the index and columns then you can use ix to achieve this:

In [19]:
df.ix['2010-01-01':, 'DewPoint':]

Out[19]:
                     DewPoint  Pressure
Date                                   
2010-01-01 00:00:00      37.5       1.0
2010-01-01 01:00:00      37.1       1.0
2010-01-01 02:00:00      36.9       1.0
2010-01-01 03:00:00      36.9       1.0
2010-01-01 04:00:00      36.8       1.0

The docs detail numerous ways of selecting data

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.