I want to creat list thats counts the iterration of zeros for every same rows in a dataframe.
Id_1 Id_2
0 1401 1
1 1401 1
2 1801 0
3 1801 0
4 1801 0
5 1801 0
6 2001 1
7 2001 1
8 2201 0
9 2201 0
# I would like this output:
L = [(1801, 4), (2201, 2)]
CodePudding user response:
You can do the job in one line like this:
L = list(df[df['Id_2'] == 0].groupby(['Id_1']).count().to_records())
output:
[(1801, 4), (2201, 2)]
CodePudding user response:
Filter values by DataFrame.loc
with Series.value_counts
and convert Series
to list of tuples:
L = [(a, b) for a, b in df.loc[df['Id_2'].eq(0), 'Id_1'].value_counts().items()]
print (L)
[(1801, 4), (2201, 2)]
Or:
L = list(df.loc[df['Id_2'].eq(0), 'Id_1'].value_counts().to_frame().to_records())
print (L)
[(1801, 4), (2201, 2)]
Or:
L = (df.loc[df['Id_2'].eq(0), 'Id_1']
.value_counts()
.to_frame(0)
.set_index(0, append=True)
.index
.tolist())
print (L)
[(1801, 4), (2201, 2)]