1

Let's Say I have 3 series

>>> df[df['Type']=="Machine Learning"]['Cost']
0     2300.00
1     3200.00
4     1350.00
7     1352.00
8     4056.00
9       79.00
10    1595.00
Name: Cost, dtype: float64
>>>df[df['Type']=="Machine Learning"]['Rank']
0      1
1      1
4      1
7      2
8      2
9      2
10     2
Name: Rank, dtype: int64
>>>df[df['Type']=="Machine Learning"]['Univ/Org']
0     Massachusetts Institute of Technology 
1     Massachusetts Institute of Technology 
4                                    EDX/MIT
7                        Stanford University
8                        Stanford University
9               Coursera/Stanford University
10                       Stanford University
Name: Univ/Org, dtype: object

Now I want to draw a scatter plot with Cost at the y-axis, Rank at the X-axis, and Name of Univ/Org at each data point.

Now What I am able to do yet after referring to this question is

plt.scatter(df[df['Type']=="Machine Learning"]['Rank'], df[df['Type']=="Machine Learning"]['Cost'],marker='2', edgecolors='black')
for i, txt in enumerate(df[df['Type']=="Machine Learning"]['Univ/Org']):
    plt.annotate(txt, (df[df['Type']=="Machine Learning"]['Rank'][i], df[df['Type']=="Machine Learning"]['Cost'][i]))

It is naming 2 data points and then giving an error.

Plot is : enter image description here

And Error is:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-111-0d31107a166a> in <module>
      1 plt.scatter(df[df['Type']=="Machine Learning"]['Rank'], df[df['Type']=="Machine Learning"]['Cost'],marker='2', edgecolors='black')
      2 for i, txt in enumerate(df[df['Type']=="Machine Learning"]['Univ/Org']):
----> 3     plt.annotate(txt, (df[df['Type']=="Machine Learning"]['Rank'][i], df[df['Type']=="Machine Learning"]['Cost'][i]))

~/anaconda3/lib/python3.8/site-packages/pandas/core/series.py in __getitem__(self, key)
    869         key = com.apply_if_callable(key, self)
    870         try:
--> 871             result = self.index.get_value(self, key)
    872 
    873             if not is_scalar(result):

~/anaconda3/lib/python3.8/site-packages/pandas/core/indexes/base.py in get_value(self, series, key)
   4403         k = self._convert_scalar_indexer(k, kind="getitem")
   4404         try:
-> 4405             return self._engine.get_value(s, k, tz=getattr(series.dtype, "tz", None))
   4406         except KeyError as e1:
   4407             if len(self) > 0 and (self.holds_integer() or self.is_boolean()):

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

KeyError: 2
2
  • you're not accessing the elements of your dataframe properly. Commented Aug 31, 2020 at 16:40
  • In other words, 2 isn't in your index. use iloc for positional based indexing Commented Aug 31, 2020 at 16:42

1 Answer 1

1

Couple of things.

First I recommend you select you ML data in to a new dataframe. You should also use the .loc and .at accessors to be a little more precise. So like this:

mldf = df.loc[df['Type'] == "Machine Learning", :]

fig, ax = plt.sunplots()
ax.scatter('Rank', 'Cost', data=mldf, marker='2', edgecolors='black')
for i in mldf.index:
    ax.annotate(mldf.at[i, 'Univ/Org'], (mldf.at[i, 'Rank'], mldf.at[i, 'Cost'])
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, I am using a bit big dataset, so Text is overlapping to each other, Can you suggest how to overcome that.
@AhmadAnis I would change the text
You mean the first parameter in ax.annotate?
@AhmadAnis yeah

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.