import pandas as pd
# For Series (columns)
def get_total_occurrences_of_numbers_in_column(s):
count = 0
index = 0
size = s.size
d = {}
# print(size)
while index < size:
c = 0
# print(df.nums[index])
while (index < size) and (s[index] < 0): #
c = c + 1
index += 1
if c > 1:
count += 1
if c in d: # {}
d[c] += 1 # {3: 2} => 3 consecutives 2 times...continue with next check
else:
d[c] = 1; # {3: 1} => 3 consectives 1 time...continue with next check
index += 1
return (count, d)
def get_counts(df):
counts = []
indices = []
all_counts = {}
for column in df.columns:
indices.append(column)
count, d = get_total_occurrences_of_numbers_in_column(df[column])
all_counts[column] = d; # {'col1': {2: 3, 4: 2}} => for col1, 2 consecutives 3 times, 4 consecutives 2 times
counts.append(count)
new_df = pd.DataFrame(counts, index=indices, columns=["count"])
return (new_df, all_counts)
# -ve numbers
col1 = [12, 11, -4, -3, -1, 0, -3, -2, 9, -1, 2, -3, 5] # 2 times (13 numbers)
col2 = [-1, -3, -6, -7, 8, -5, -3, 9, 12, 10, -4, -6, -6] # 3 times
col3 = [12, 4, 5, 6, 4, 1, 2, 3, 4, 5, 3, 9, 0] # 0 times
col4 = [-1, 2, -3, 4, 5, -6, 7, -8, 9, -10, 11, -12, 13] # 0 times
col5 = [-12, -3, 4, 3, -5, -6, 0, -4, -5, 6, -6, -7, 0] # 4 times
df3 = pd.DataFrame({'col1': col1, "col2": col2, "col3": col3, "col4": col4, "col5": col5})
df3 # same as MATLAB, add ; at the end to suppress the output
print(df3)
df, all_counts = get_counts(df3)
print(df)
print(all_counts)