Home > Mobile >  how to count data in a certain column in python(pandas)?
how to count data in a certain column in python(pandas)?

Time:06-02

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

CodePudding user response:

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

CodePudding user response:

IIUC,

count = (df['Color'].eq('green') & df['Color'].shift().eq('green')).sum()

CodePudding user response:

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
  • Related