如何将左表转换为汇总表?

huangapple go评论84阅读模式
英文:

How can I convert a left table into a summary table?

问题

如何将左侧的表格转换为右侧的汇总表格?

我尝试使用 "get_dummies" 函数将值转换为 0 和 1。
之后我不知道该如何继续。

英文:

How do I convert table left to summary table right?

如何将左表转换为汇总表?

I tried using get dummies function to convert values to 0 and 1.
I don't know how to proceed after that.

答案1

得分: 1

请尝试以下代码:

import pandas as pd
import numpy as np
col1 = ['']+['Hampshire']*8+['']+['Hampshire']+['']+['Hampshire']+['','']+['Hampshire']*4
col2 = ['Southhampton'] + ['']*12 + ['Southhampton']*2 + ['']*4
col3 = ['']*11 + ['Isle of Wight'] + ['']*7
col4 = ['Met']*5 + [''] + ['Met']*13
col5 = ['']*5 + ['Partially met'] + ['']*13
col6 = ['']*19

df = pd.DataFrame(data = dict(zip(['Hampshire', 'Southhampton', 'Isle of Wight', '5met', '5partially met', '5Not met'],[col1,col2,col3,col4,col5,col6])))
df = df.replace('', np.nan)
df['Hampshire'] = df['Hampshire'].fillna(df['Southhampton'])
df['Hampshire'] = df['Hampshire'].fillna(df['Isle of Wight'])
df[['Hampshire','5met','5partially met', '5Not met']].groupby(by=['Hampshire']).count()

我为您生成了代码,因为您没有提供除了图像之外的数据,但我认为这可以完成任务。希望这有所帮助。

英文:

Try this:

import pandas as pd
import numpy as np
col1 = ['']+['Hampshire']*8+['']+['Hampshire']+['']+['Hampshire']+['','']+['Hampshire']*4
col2 = ['Southhampton'] + ['']*12 + ['Southhampton']*2 + ['']*4
col3 = ['']*11 + ['Isle of wight'] + ['']*7
col4 = ['Met']*5 + [''] + ['Met']*13
col5 = ['']*5 + ['Partially met'] + ['']*13
col6 = ['']*19

df = pd.DataFrame(data = dict(zip(['Hampshire', 'Southhampton', 'Isle of wight', '5met', '5partially met', '5Not met'],[col1,col2,col3,col4,col5,col6])))
df = df.replace('', np.nan)
df['Hampshire'] = df['Hampshire'].fillna(df['Southhampton'])
df['Hampshire'] = df['Hampshire'].fillna(df['Isle of wight'])
df[['Hampshire','5met','5partially met', '5Not met']].groupby(by=['Hampshire']).count()

I had to generate the data for you (since you didn't post any besides the image), but I think this get's the job done. I hope this helps.

答案2

得分: 0

使用 crosstab 在使用 stack 重新排列两个列块之后:

s1 = df[['Hampshire', 'Southhampton', 'Isle of wight']].stack().droplevel(-1)
s2 = df[['5met', '5partially met']].stack().droplevel(-1)

out = (pd.crosstab(s1, s2)
         .reindex(columns=['Met', 'Partially met', 'Not met'], fill_value=0)
         .rename_axis(columns=None, index=None)
       )

输出:

               Met  Partially met  Not met
Hampshire       13              1        0
Isle of wight    1              0        0
Southhampton     3              0        0
英文:

Using a crosstab after reshaping the two blocks of columns with stack:

s1 = df[['Hampshire', 'Southhampton', 'Isle of wight']].stack().droplevel(-1)
s2 = df[['5met', '5partially met']].stack().droplevel(-1)

out = (pd.crosstab(s1, s2)
         .reindex(columns=['Met', 'Partially met', 'Not met'], fill_value=0)
         .rename_axis(columns=None, index=None)
       )

Output:

               Met  Partially met  Not met
Hampshire       13              1        0
Isle of wight    1              0        0
Southhampton     3              0        0

huangapple
  • 本文由 发表于 2023年6月1日 20:27:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76381858.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定