在熊猫中创建列联表

时间:2018-10-06 14:07:49

标签: python python-3.x pandas dataframe contingency

我想在熊猫中创建一个列联表。我可以使用下面的代码来做到这一点,但我想知道是否有一个熊猫函数可以帮我做到这一点。

有关可重现的示例:

toy_data #json
'{"Light":{"321":"no_light","476":"night_light","342":"lamp","454":"lamp","25":"night_light","53":"night_light","120":"night_light","346":"night_light","360":"lamp","55":"no_light","391":"night_light","243":"no_light","101":"night_light","377":"night_light","124":"no_light","368":"lamp","400":"no_light","247":"night_light","270":"lamp","208":"night_light"},"Nearsightedness":{"321":"No","476":"Yes","342":"Yes","454":"Yes","25":"No","53":"Yes","120":"Yes","346":"No","360":"No","55":"Yes","391":"Yes","243":"No","101":"No","377":"Yes","124":"No","368":"No","400":"No","247":"No","270":"Yes","208":"No"}}'

toy_data.head()
    Light       Nearsightedness
321 no_light       No
476 night_light    Yes
342 lamp           Yes
454 lamp           Yes
25  night_light    No

df = pd.DataFrame(toy_data.groupby(['Light', 'Nearsightedness']).size())

df = df.unstack('Nearsightedness')

df.columns = df.columns.droplevel()

df
Nearsightedness No  Yes
Light       
lamp             2  3
night_light      5  5
no_light         4  1

您的建议将不胜感激。

2 个答案:

答案 0 :(得分:1)

您可以使用pd.crosstab

res = pd.crosstab(df['Light'], df['Nearsightedness'].eq('Yes'))

print(res)

Nearsightedness  False  True 
Light                        
lamp                 2      3
night_light          5      5
no_light             4      1

答案 1 :(得分:1)

pd.crosstab将达到目的:

pd.crosstab(df.Light, df.Nearsightedness)

输出:

Nearsightedness  No  Yes
Light
lamp              2    3
night_light       5    5
no_light          4    1