Reputation: 113
By using .value_counts()
on my dataframe I got a series as follows:
Sample data:
In_A_Not_In_B 116
In_C_Not_In_B 104
In_A_Not_In_C 100
In_C_Not_In_A 85
I need to create a matrix out of the above data as shown below. The index names have to be fetched from the column value.
Expected output:
NOT IN
A B C
A NA 116 100
IN C 85 104 NA
So far I have tried with .pivot_table()
, .MultiIndex()
with no luck.
Upvotes: 4
Views: 439
Reputation: 27879
Let's say that your result of value_counts
is stored in variable df
, then:
df = df.reset_index()
df['In'] = df['index'].str[3]
df['Not In'] = df['index'].str[-1]
df.pivot(index='In', columns='Not In', values=0)
# Not In A B C
# In
# A NaN 116.0 100.0
# C 85.0 104.0 NaN
Upvotes: 4