hope you’re doing well . i tried counting green color row after another green colored row in the table below In [1]: df = pd.DataFrame([[green], [red], [red]], columns=[‘A’])
the code i tried to count greengreen:
for index,row in data.iterrows(): if finalData['Color'].loc[i]=='green' & finalData['Color'].loc[i+1]=='green': greengreen+=1 i+=1
but it didn’t work,hope you can help. note: i’m new to data science
Advertisement
Answer
You can use:
# is the color green? m = df['color'].eq('green') # count the matches that precede another match greengreen = (m&m.shift()).sum()
As a one-liner (python ≥ 3.8):
greengreen = ((m:=df['color'].eq('green'))&m.shift()).sum()
example input:
df = pd.DataFrame({'color': ['green', 'green', 'green', 'red', 'green', 'red', 'green', 'green']})
output: 3