Why does jupyter notebook returns an error the second time I run the cell?

Screen Link:

My Code:

tag_counts = pd.DataFrame.from_dict(tag_counts, orient="index")
tag_counts.rename(columns={0: "Count"}, inplace=True)

What I expected to happen:

lasso 8
activation-function 44
notation 4
sas 6
binary 26

What actually happened:

TypeErrorTraceback (most recent call last)
<ipython-input-6-4374bb43fc2a> in <module>()
----> 1 tag_counts = pd.DataFrame.from_dict(tag_counts, orient="index")
      2 tag_counts.rename(columns={0: "Count"}, inplace=True)
      3 tag_counts.head()

/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/frame.py in from_dict(cls, data, orient, dtype)
    891             if len(data) > 0:
    892                 # TODO speed up Series case
--> 893                 if isinstance(list(data.values())[0], (Series, dict)):
    894                     data = _from_nested_dict(data)
    895                 else:

TypeError: 'numpy.ndarray' object is not callable

When I ‘restart & run all’ the Kernel things go as expected, but if I re-run the cell, this error shows up and I’m having a hard time understanding why, could anyone help me, please?

It’s because you are using the same name for converting the dictionary to a DataFrame - tag_counts.

When you run the code cell once, tag_counts changes from a dictionary to a DataFrame.

When you try to run it again, you are trying to convert a dictionary to a DataFrame, but since tag_counts is already a DataFrame from the first time you ran the code, it throws an error since it expects a dictionary instead.

1 Like

Now I get it, thank you!

1 Like