Combining "used" and "viewed" counts of tags

Screen Link:

My Code:

tag_count = pd.DataFrame(columns=["used", "viewed"])
tag_count["viewed"] = tag_count["viewed"].astype(int)

for index,row in questions.iterrows():
    for row in row["Tags"]:
        if tag in tag_count.index:
            tag_count.loc[tag, "used"] += 1
            tag_count.loc[tag, "viewed"] += row["ViewCount"]
            tag_count.loc[tag, "used"] = 1
            tag_count.loc[tag, "viewed"] = row["ViewCount"]

I was trying to combine used counts and viewed counts into one dataframe at the same time but I’m getting this error and I’m not really sure why:

TypeErrorTraceback (most recent call last)
<ipython-input-35-9a21ba05fd3b> in <module>()
      9         else:
     10             tag_count.loc[tag, "used"] = 1
---> 11             tag_count.loc[tag, "viewed"] = row["ViewCount"]

TypeError: string indices must be integers

So I try to change row["ViewCount"] to row[3] but I get this error instead:

<ipython-input-13-2cfb8596892d> in <module>()
      6         if tag in tag_count.index:
      7             tag_count.loc[tag, "used"] += 1
----> 8             tag_count.loc[tag, "viewed"] += row[3]
      9         else:
     10             tag_count.loc[tag, "used"] = 1

IndexError: string index out of range

Any ideas? Thank you!


In the first line of the above code snippet, the datatype of row is pandas.Series. In the second line, you are using the same variable name, row, and the data type this time is a string.

Python replaces the pandas.Series with the string row. It recognizes what you assigned/used last.

You want to access the string, row, with ViewCount. You do this with integer values / index.

1 Like

I realize now that my mistake was
for row in row["Tags"]
instead of using
for tag in row["Tags"]

Thank you.

1 Like