Pandas: Transform dataframe to show if a combinati

2020-03-24 03:19发布

I have a Dataframe that looks like this:

 | Col 1 | Col 2 | 
0|   A   |   2   |
1|   A   |   3   |
2|   B   |   1   |
3|   B   |   2   |

and I need to transform it into a Dataframe that shows for each combination, of the values in Col 1 and Col 2 if that combination is contained in the original DataFrame:

  |  1  |  2  |  3  |
A |False|True |True |
B |True |True |False|

Is there a native way in pandas to get this transformation? I was creating the transformed Dataframe manually, but this is way to slow.

Thank you in advance!

标签: python pandas
3条回答
何必那么认真
2楼-- · 2020-03-24 03:57

Here's a pivot solution:

(df.pivot('Col 1', 'Col 2', 'Col 1').fillna(0) != 0).rename_axis(index=None, columns=None)
         1     2      3
A      False  True   True
B       True  True  False
查看更多
家丑人穷心不美
3楼-- · 2020-03-24 04:01

You could use:

df.groupby(['Col 1','Col 2']).size().unstack(fill_value=0).astype(bool)

Col2      1     2      3
Col1                    
A     False  True   True
B      True  True  False
查看更多
我欲成王,谁敢阻挡
4楼-- · 2020-03-24 04:02

Use get_dummies with max:

df = pd.get_dummies(df.set_index('Col 1')['Col 2'], dtype=bool).rename_axis(None).max(level=0)
print (df)
       1     2      3
A  False  True   True
B   True  True  False

Or if possible not missing values in column Col2 then use DataFrame.pivot with DataFrame.notna, for remove index and columns name use DataFrame.rename_axis:

df = df.pivot('Col 1', 'Col 2', 'Col 1').notna().rename_axis(index=None, columns=None)
print (df)
       1     2      3
A  False  True   True
B   True  True  False

Alternative is possible duplicates and pivot failed:

df = (df.pivot_table(index='Col 1', columns='Col 2', values='Col 1', aggfunc='size')
        .notna()
        .rename_axis(index=None, columns=None))
print (df)
       1     2      3
A  False  True   True
B   True  True  False

Or solution from comments:

df = (pd.crosstab(df['Col 1'], df['Col 2'])
        .gt(0)
        .rename_axis(index=None, columns=None))
查看更多
登录 后发表回答