0

I have Pandas DataFrame:

df  = pd.read_csv("file.csv")

I need to count not all occurancies of columnn 'gender', but for the first 5 entries (would love to see code for any interval of rows, let's say from 10th to the 20th etc.)

Currently I am familliar only with this:

df[df['gender'] == 1].shape[0]

and more complicated with lambda:

A2_object = df.apply(lambda x: True if x['gender'] == 1 else False, axis=1)
A2 = len(A2_object[A2_object == True].index)

I am learning, and I see that loops don't work in dataframe the same way as in the lists or dictionaries.

I am trying something like that:

df[df['gender'] == 1 and df.index < 5].shape[0]

I love this post, but can't get my head around the examples.

cabavyras
  • 59
  • 7

1 Answers1

0

As Mr. @Quang-Hoang posted, I needed to use slicing for indecies and in dataframe format indecies are .iloc (.loc?). Thank you, Sir.

Answer: df.iloc[start:end]['gender'].eq(1).sum()

cabavyras
  • 59
  • 7