I am trying to get the first element throughout multiple column and count how many times the value appear.
A B
1,2,3 23,4,5
2 54
2 2
result
1 1
2 3
54 1
32 1
Use DataFrame.stack
for Series
, then Series.str.split
with selecting first values by indexing, convert to integer if necessary and count by Series.value_counts
, if necessary sorting last:
s = df.stack().str.split(',').str[0].astype(int).value_counts().sort_index()
print (s)
1 1
2 3
23 1
54 1
dtype: int64
If need 2 columns DataFrame:
df1 = (df.stack()
.str.split(',')
.str[0]
.astype(int)
.value_counts()
.sort_index()
.rename_axis('result')
.reset_index(name='counts'))
print (df1)
result counts
0 1 1
1 2 3
2 23 1
3 54 1