1

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

Nidal Zd
  • 91
  • 7
  • can you please remove the image and provide the example data as text instead? (`print(df.head())`) – mozway Jun 02 '22 at 12:00

3 Answers3

3

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

mozway
  • 194,879
  • 13
  • 39
  • 75
  • thank you it worked, can it be used for red green or green red? – Nidal Zd Jun 02 '22 at 12:06
  • Yes but you need to use two masks then (maybe open a new question with an example of what you want exactly, please follow [these guidelines](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) for your example). – mozway Jun 02 '22 at 12:08
1
data = {'col1': ['A','B','C','D'],\
        'col2': ['green','green', 'red','green']}
df = pd.DataFrame(data) 
df
index col1 col2
0 A green
1 B green
2 C red
3 D green
df.col2.values
greengreen = 0
greenred = 0
redgreen = 0

for i in range(len(df.col2.values)):
  if i < (len(df.col2.values)-1): 
    if df.col2.values[i] == 'green' and df.col2.values[i+1] == 'green':
      greengreen += 1
    elif df.col2.values[i] == 'green' and df.col2.values[i+1] == 'red':
      greenred += 1
    elif df.col2.values[i] == 'red' and df.col2.values[i+1] == 'green':
      redgreen += 1
    else:
      print('?')

print(greengreen, greenred, redgreen)
1 1 1
Drakax
  • 1,305
  • 3
  • 9
1

IIUC,

count = (df['Color'].eq('green') & df['Color'].shift().eq('green')).sum()
Ynjxsjmh
  • 28,441
  • 6
  • 34
  • 52