1

I know this is an ancient question and I have searched all previous posts but cannot quite find answers that address my specific question.

I am dealing with a dataframe that looks like this

   Date    Position  Value
2010-01-01 PEAK      600
2010-01-01 BOTTOM    510
2010-01-02 PEAK      620
2010-01-02 BOTTOM    500
...
2015-03-02 PEAK      700

which contains no nan values or weird characters. However, the original data is not of "date" format, so I converted it to "date" format using

df1 = df.sort('Date')
df1['Date'] = pd.to_datetime(df1['Date'])

but after I tried to group my dataframe using groupby() command

df1.groupby('Date')

before grouping the "Position" column and performing calculation on "Value," I keep getting error message that shows

<pandas.core.groupby.DataFrameGroupBy object at 0x7f880d4f2160>

but nothing generated.

I was expecting my code would group all row entries with the same datetime value together, but this didn't turn out that way.

I would greatly appreciate if someone could shed some light on this.

Thank you.

1
  • Please accept if right! I love that juicy reputation Commented Mar 17, 2017 at 19:35

1 Answer 1

2

This is not an error. groupby returns a groupby object, which then you have to call an aggregating function on. Usually people call .mean() on it, tho there are others that maybe someone else can fill you in on.

Since I don't know exactly what you want, I'll take a guess with my example

 df1.groupby(['Date', 'Position']).mean()

Will output a dataframe grouped by date and position, with the PEAK and BOTTOM values averaged for all PEAK and BOTTOM values, respectively, for that particular day.

Oh wait there's only a peak and bottom value for a particular day. Try

df1[['Date', 'Value']].groupby('Date').mean()

That averages the values for that day.

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

2 Comments

Yes, this works. Thanks so much for your prompt reply. I need to read document more thoroughly though.
@ChrisT. Anytime!

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.