What I'm trying to do is create a function that returns the frequency or count of a name input into the function. For example, if I inputted 'Olivia' into my get_frequency(name) function I would want 19674 returned.
Asked
Active
Viewed 229 times
-1
-
Please supply the expected [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) (MRE). We should be able to copy and paste a contiguous block of your code, execute that file, and reproduce your problem along with tracing output for the problem points. This lets us test our suggestions against your test data and desired output. Images of text are not acceptable; we need your question to be self-contained, in keeping with the purpose of this site. – Prune Apr 08 '21 at 22:19
-
Please [include a minimal data frame](https://stackoverflow.com/questions/52413246/how-to-provide-a-reproducible-copy-of-your-dataframe-with-to-clipboard) as part of your MRE. – Prune Apr 08 '21 at 22:19
2 Answers
0
In your sintax above:
df.loc[df.Name == 'name', 'Count]
or you can use at
df.at[row_index, col_index]
will return the scalar of the position, or you can use the index number:
df.iat[row_pos_number, col_pos_number]

neves
- 33,186
- 27
- 159
- 192
0
You don't actually need this if condition, you could just search for the 'Count' value when df['name'] is equal to the name you are searching for.
snippet of code:
def get_frequency(name):
return df['Count'][df['Name']==name]
if you want the scalar value you could use values like this:
def get_frequency(name):
return df['Count'][df['Name']==name].values[0]
as a result you'd get:
get_frequency('Emma')
>> 20799

Túlio Chiodi
- 43
- 1
- 1
- 7