0

I need to find the maximum value among values in a list. Every element of this list is a pd.Series so when I use the function max(list) I get this error:

"ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()."

How can I avoid this problem?

From a Dataframe that look like this:

             Date                Actual 
0     2016-12-30 12:30:00          1800
1     2016-12-30 13:00:00          1800
2     2016-12-30 13:30:00          1600 
.
.
.
1256   201-05-30 13:30:00          1500

I create my list:

for single_date in datetime_range(start_date, end_date, timedelta(minutes=30)):
        b = find_nearest(df['Date'], single_date)
        row = df4[df4['Date'] == b ].index.tolist()  
        en=(df4.iloc[row]['Actual (kW)'])
        energy.append(en)

I need to find the maximum value in the list energy. Thank you

  • 4
    Welcome to StackOverflow. Please take the time to read this post on [how to provide a great pandas example](http://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) as well as how to provide a [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve) and revise your question accordingly. These tips on [how to ask a good question](http://stackoverflow.com/help/how-to-ask) may also be useful. – jezrael Apr 25 '18 at 11:27
  • What does your dataframe look like? – Xantium Apr 25 '18 at 11:27
  • Sorry, I am basically a beginner. Every element of my list is taken from a Dataframe look like this: ` Date Actual (kW) 0 2016-12-30 12:30:00 180 1 2016-12-30 13:00:00 180 2 2016-12-30 13:30:00 180` – Arianna Sorrentino Apr 25 '18 at 11:34
  • IIUC `np.array(list_name).max()`. – shivsn Apr 25 '18 at 11:37
  • @shivsn I obtain < built-in method max of numpy.ndarray object at 0x000001FEAD6655D0 > – Arianna Sorrentino Apr 25 '18 at 11:40
  • @AriannaSorrentino you will have to post a sample and desired output. – shivsn Apr 25 '18 at 11:42
  • @AriannaSorrentino It is not clear what "the maximum value among values in a list" is when the list items are `Series`. – Stop harming Monica Apr 25 '18 at 11:45
  • `np.max(energy)` should work. – shivsn Apr 25 '18 at 12:07
  • I obtain `code`dataframe nan C:\Users\arianna\Anaconda3\lib\site-packages\numpy\core\_methods.py:26: RuntimeWarning: invalid value encountered in reduce return umr_maximum(a, axis, None, out, keepdims)`code` – Arianna Sorrentino Apr 25 '18 at 12:09

2 Answers2

0

In order to calculate the maximum of a list, Python needs to compare the elements within the list. In your case, each element of the list is a pandas Series. And the error comes from the fact that there is no single True/False answer in the comparison of two series (as the comparison is done element by element).

How do you want to determine if one pandas Series is bigger than the other?

If I assume that each Series has only one value, you can do:

max(list_name, key=lambda x: x.values[0])

In general, in key you specify a function that is applied to the elements of the list before the comparison, hence here I passed a function that takes the first (and only) value from each Series.

FLab
  • 7,136
  • 5
  • 36
  • 69
0

Since you are working with pandas Dataframes, you can use these objects to find the maximum value...

  • ... of the whole frame, i.e. the column-wise maxima; in a frame with m columns you'd get m maximal values
  • ... of individual columns

For both, see here as reference.

So in your case, you should be able to simply do:

df['Actual'].max()

where df is the dataframe from your example.

If you have to filter down to a range of values, that is possible too. Consider this (arbitrary) example for the syntax:

df[df['Foo'] >= 3]['Bar'].max()

This will take column 'Foo', select everything where 'Foo' is larger-or-equal to 3, and then take column 'Bar' and print the maximum for only that filtered range of 'Bar'.

I suggest you take a look at how selection is done in Pandas, there's some neat examples there!

Oliver Baumann
  • 2,209
  • 1
  • 10
  • 26