[Pandas] Dataframe의 값을 유일하게 식별하고 카운트하기
import pandas as pd
df = pd.DataFrame({'A':[1,2,3,2,3,2], 'B':[2,3,4,3,4,3], 'C':[3,4,5,4,5,4]})
df1 = df.apply(pd.Series.value_counts)
df1
A B C
1 1.0 NaN NaN
2 3.0 1.0 NaN
3 2.0 3.0 1.0
4 NaN 2.0 3.0
5 NaN NaN 2.0
df2 = df1.fillna(0) # Nan to 0
df2
A B C
1 1.0 0.0 0.0
2 3.0 1.0 0.0
3 2.0 3.0 1.0
4 0.0 2.0 3.0
5 0.0 0.0 2.0
df2['D'] = df2['A'] + df2['B'] + df2['C']
df2
A B C D
1 1.0 0.0 0.0 1.0
2 3.0 1.0 0.0 4.0
3 2.0 3.0 1.0 6.0
4 0.0 2.0 3.0 5.0
5 0.0 0.0 2.0 2.0
댓글
댓글 쓰기