Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas max value index

I have a Pandas DataFrame with a mix of screen names, tweets, fav's etc. I want find the max value of 'favcount' (which i have already done) and also return the screen name of that 'tweet'

df = pd.DataFrame()
df['timestamp'] = timestamp
df['sn'] = sn
df['text'] = text
df['favcount'] = fav_count


print df
print '------'
print df['favcount'].max()

I cant seem to find anything on this, can anyone help guide me in the right direction?

like image 593
mGarsteck Avatar asked Oct 10 '16 18:10

mGarsteck


People also ask

How do you get the highest value in pandas?

Pandas DataFrame max() Method The max() method returns a Series with the maximum value of each column. By specifying the column axis ( axis='columns' ), the max() method searches column-wise and returns the maximum value for each row.

How do you find the index of a column with maximum value?

To get the position of the maximum value in a range (i.e. a list, table, or row), you can use the MAX function together with the MATCH function. Which returns the number 4, representing the position in this list of the the most expensive property. The MAX function first extracts the maximum value from the range C3:C11.

How do you find the maximum index of a series in Python?

idxmax() function to find the index label corresponding to the maximum value in the series. Output : As we can see in the output, the Series. idxmax() function has returned the index label of the maximum element in the given series object.

Is there a limit to pandas DataFrame?

The long answer is the size limit for pandas DataFrames is 100 gigabytes (GB) of memory instead of a set number of cells.


2 Answers

Use argmax() idxmax() to get the index of the max value. Then you can use loc

df.loc[df['favcount'].idxmax(), 'sn']

Edit: argmax() is now deprecated, switching for idxmax()

like image 75
Steven G Avatar answered Oct 19 '22 21:10

Steven G


I think you need idxmax - get index of max value of favcount and then select value in column sn by loc:

df = pd.DataFrame({'favcount':[1,2,3], 'sn':['a','b','c']})

print (df)
   favcount sn
0         1  a
1         2  b
2         3  c

print (df.favcount.idxmax())
2

print (df.loc[df.favcount.idxmax()])
favcount    3
sn          c
Name: 2, dtype: object

print (df.loc[df.favcount.idxmax(), 'sn'])
c
like image 38
jezrael Avatar answered Oct 19 '22 22:10

jezrael