1

I'm new to Python and the Pandas module, but I can't seem to get this to work.

This is my code. I'm using a csv file containing the month and rainfall for Singapore.

Below is my code: 0

df = pd.read_csv('rainfall-monthly-total.csv')

print ((df['total_rainfall'])[df.total_rainfall == df['total_rainfall'].max()])
print ((df['month'])[df.total_rainfall == df['total_rainfall'].max()])
print ((df['total_rainfall', 'month'])[df.total_rainfall == df['total_rainfall'].max()])

The first two statements work fine. But something is wrong with the third and I can't find out why. Below is the output.

"/Users/xxxx/PycharmProjects/Phyton for Finance/venv/bin/python" "/Users/xxxx/PycharmProjects/Phyton for Finance/Panda Tutorial.py"
299    765.9
Name: total_rainfall, dtype: float64
299    2006-12
Name: month, dtype: object
Traceback (most recent call last):
  File "/Users/xxxx/PycharmProjects/Phyton for Finance/venv/lib/python3.7/site-packages/pandas/core/indexes/base.py", line 3078, in get_loc
    return self._engine.get_loc(key)
  File "pandas/_libs/index.pyx", line 140, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 1492, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 1500, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: ('total_rainfall', 'month')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/xxxx/PycharmProjects/Phyton for Finance/Panda Tutorial.py", line 16, in <module>
    print ((df['total_rainfall', 'month'])[df.total_rainfall == df['total_rainfall'].max()])
  File "/Users/xxxx/PycharmProjects/Phyton for Finance/venv/lib/python3.7/site-packages/pandas/core/frame.py", line 2688, in __getitem__
    return self._getitem_column(key)
  File "/Users/xxxx/PycharmProjects/Phyton for Finance/venv/lib/python3.7/site-packages/pandas/core/frame.py", line 2695, in _getitem_column
    return self._get_item_cache(key)
  File "/Users/xxxx/PycharmProjects/Phyton for Finance/venv/lib/python3.7/site-packages/pandas/core/generic.py", line 2489, in _get_item_cache
    values = self._data.get(item)
  File "/Users/xxxx/PycharmProjects/Phyton for Finance/venv/lib/python3.7/site-packages/pandas/core/internals.py", line 4115, in get
    loc = self.items.get_loc(item)
  File "/Users/xxxx/PycharmProjects/Phyton for Finance/venv/lib/python3.7/site-packages/pandas/core/indexes/base.py", line 3080, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))
  File "pandas/_libs/index.pyx", line 140, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 1492, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 1500, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: ('total_rainfall', 'month')

Process finished with exit code 1

I'm using PyCharm with python 3.7. How do I get python to print out both columns for that particular month?

2
  • 2
    Please post code snippets not code snapshots. Commented Dec 3, 2018 at 10:51
  • Edited. Hopefully it's more convenient now. Commented Dec 3, 2018 at 11:09

2 Answers 2

3

Try this:

print ((df[['total_rainfall', 'month']])[df.total_rainfall == df['total_rainfall'].max()]

You need to convert single square brackets to double:

['total_rainfall', 'month']

TO

[['total_rainfall', 'month']]
Sign up to request clarification or add additional context in comments.

Comments

0

Easy. You need to use use a list of columns you want to print. so use df.loc to filter your data frame with conditions:

print(df.loc[df.total_rainfall == df['total_rainfall'].max(), ['total_rainfall', 'month']])

1 Comment

Yes, but that doesn't print out the month with the maximum rainfall, which is what I wanted. My above code is similar, but it runs into error.

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.