0
$\begingroup$

As soon as I add header = None, I get the error TypeError: 'int' object is not subscriptable. Any help on how to solve this without removing header = None from reader?

Code:

reader = pd.read_csv('counts.csv', header = None)

X3 = []
y3 = []
for row in reader:
    label = row[2]
    if len(label) > 0 and label.find(',') == -1:
        y3.append(label)
y3 = np.asarray(y3)
encoder = LabelEncoder()
encoder.fit(y3)
encoded_y = encoder.transform(y3)
counts = np.bincount(encoded_y)
print(counts)
fig, ax = plt.subplots()
plt.bar(list(range(6)), counts)
ax.set_xticklabels(('', 'plasma', 'lymphocyte', 'epithelial', 'neutrophil','eosinophil','connective'))
ax.set_ylabel('Counts')

Error:

TypeError                                 Traceback (most recent call last)
<ipython-input-97-53828df7d817> in <module>
      7 y3 = []
      8 for row in reader:
----> 9     label = row[2]
     10     if len(label) > 0 and label.find(',') == -1:
     11         y3.append(label)

TypeError: 'int' object is not subscriptable

$\endgroup$
1
  • $\begingroup$ For future reference, please ask programming questions on stackoverflow.com. $\endgroup$
    – Erwan
    Mar 26, 2022 at 13:01

1 Answer 1

0
$\begingroup$

reader is a dataframe, since you want to iterate the rows you need to use the function iterrows:

import pandas as pd

reader = pd.read_csv('counts.csv', header = None)

for rowno, row in reader.iterrows():
    label = row[2]
    print('row ',rowno,' label:', label)    

See for instance here about iterating dataframes.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.