0

I am trying to access a column by its index number in a Pandas data-frame using iloc:

 new = df_new.iloc[:,0]

throws out the error :

IndexingError: Too many indexers

Other posts on iloc show them working fine. Don't understand why is this happening with me . Please help !!

The data-frame is :

Games                3862
Entertainment         535
Education             453
Photo & Video         349
Utilities             248
Health & Fitness      180
Productivity          178
Social Networking     167
Lifestyle             144
Music                 138
Shopping              122
Sports                114
Book                  112
Finance               104
Travel                 81
News                   75
Weather                72
Reference              64
Food & Drink           63
Business               57
Navigation             46
Medical                23
Catalogs               10

and was created by the code :

df_new=obj_df["prime_genre"].value_counts()

by count of the number of categories of another data-frame

SHREYANSHU
  • 71
  • 1
  • 10
  • 1
    Post the `df` as there are some possibilities for this – rafaelc Jul 11 '18 at 15:32
  • Posted the df. please check – SHREYANSHU Jul 11 '18 at 15:36
  • Only two columns in this new dataframe. I want to extract only the first column from the above dataframe. Used iloc for column indexing and it gives me this error – SHREYANSHU Jul 11 '18 at 15:39
  • Are you trying to extract the first **column** or first **row**? `iloc` is typically used to get rows by their index. – BenG Jul 11 '18 at 15:44
  • Can be used for column slicing as well based on index numbers [refer](https://www.shanelynn.ie/select-pandas-dataframe-rows-and-columns-using-iloc-loc-and-ix/) – SHREYANSHU Jul 11 '18 at 15:50
  • Possible duplicate of [Selecting columns in a pandas dataframe](https://stackoverflow.com/questions/11285613/selecting-columns-in-a-pandas-dataframe) – BenG Jul 11 '18 at 15:50

1 Answers1

3
obj_df["prime_genre"].value_counts()

returns a pd.Series (reference), which has only one dimension. The syntax [:, 0] means select all rows, and column with index 0. But because this is a series, it makes no sense to slice using rows/columns

Use

.iloc[0]

if you want the first element only.


If you want to keep using your df, assign this pd.Series to a column

obj_df['counts'] = obj_df["prime_genre"].value_counts()

Then you can slice using obj_df[:, 0]]


If you want the indexes, use simply

obj_df["prime_genre"].value_counts().index

or, if you want to use the iloc notation

obj_df["prime_genre"].value_counts().reset_index().iloc[:,0]
rafaelc
  • 57,686
  • 15
  • 58
  • 82