0

I have this set of dataframe:Dataframe

I can obtain the values that is 15% greater than the mean by:

df[df['Interest']>(df["Interest"].mean()*1.15)].Interest.to_string()

I obtained all values that are 15% greater than interest in their respective categories The question is how do I get the year where these values occurred without starting with:

df=df.set_index('Year") 

at the start as the function above requires my year values with df.iloc

  • Can you please copy your data frame into the question as code rather than posting a screenshot so we can help better? – TC Arlen Aug 12 '21 at 04:23
  • May i ask how to copy and paste and arrange my dataframe in a presentable way? when i copy and paste into the website its in a mess. Sorry im very new to ths site – user16527549 Aug 12 '21 at 04:39
  • df.head(10) and copy/paste as "Code Sample (Ctrl+K)" – Corralien Aug 12 '21 at 04:41
  • see [how-to-provide-a-reproducible-copy-of-your-dataframe-with-to-clipboard](https://stackoverflow.com/questions/52413246/how-to-provide-a-reproducible-copy-of-your-dataframe-with-to-clipboard) – Anurag Dabas Aug 12 '21 at 05:04

3 Answers3

0

This will return a DataFrame with Year and the Interest values that match your condition

df[df['Interest']>(df["Interest"].mean()*1.15)][['Year', 'Interest']] 
darth baba
  • 1,277
  • 5
  • 13
0

How do I get the year where these values occurred without starting with df.set_index('Year")

Use .loc:

>>> df
    Year  Dividends  Interest  Other Types     Rent  Royalties  Trade Income
0   2007    7632014   4643033       206207   626668      89715      18654926
1   2008    6718487   4220161       379049   735494      58535      29677697
2   2009    1226858   5682198       482776  1015181     138083      22712088
3   2010     978925   2229315       565625  1260765     146791      15219378
4   2011    1500621   2452712       675770  1325025     244073      19697549
5   2012     308064   2346778       591180  1483543     378998      33030888
6   2013     275019   4274425       707344  1664747     296136      17503798
7   2014     226634   3124281       891466  1807172     443671      16023363
8   2015    2171559   3474825      1144862  1858838     585733      16778858
9   2016     767713   4646350      2616322  1942102     458543      13970498
10  2017     759016   4918320      1659303  2001220     796343       9730659
11  2018     687308   6057191      1524474  2127583    1224471      19570540


>>> df.loc[df['Interest']>(df["Interest"].mean()*1.15), ['Year', 'Interest']]
    Year  Interest
0   2007   4643033
2   2009   5682198
9   2016   4646350
10  2017   4918320
11  2018   6057191
Corralien
  • 109,409
  • 8
  • 28
  • 52
0

This will return the Year :-

df.loc[df["Interest"]>df["Interest"].mean()*1.15]["Year"]

Sample code to get Year

FalsePositive
  • 11
  • 1
  • 2