Pandas split gender column to two columns
I have the following table:
https://ibb.co/DMGCgD2
I want to split (Property Type) to two column count_Single_Family and count_Multi_Family in each zip code:
I used this code but I don't know what to do next:
b=Combineddf[Combineddf['Property Type']=='Multi-Family'].count()
a=Combineddf[Combineddf['Property Type']=='Single Family'].count()
I can do this with sql but I keep trying to get it to work with pandas but no luck. Thank you for the help.
Edit*
Input:
import pandas as pd
input= pd.DataFrame({'Zip Code': [90001, 90001, 90001,90002,90002,90002],
'Total Males': [28468, 28468, 28468,43533,43533,43533],
'Total Female': [32135, 32135, 32135,54354,54354,54354],
'Property Type': ['Multi-Family', 'Multi-Family', 'Single Family','Single Family','Single Family','Multi-Family']
})
input
Desired output:
out = pd.DataFrame({'Zip Code': [90001, 90001, 90001,90002,90002,90002],
'Total Males': [28468, 28468, 28468,43533,43533,43533],
'Total Female': [32135, 32135, 32135,54354,54354,54354],
'Multi-Family': [2, 2, 2,1,1,1],
'Single Family': [1, 1, 1,2,2,2],
})
out
python pandas split pandas-groupby
add a comment |
I have the following table:
https://ibb.co/DMGCgD2
I want to split (Property Type) to two column count_Single_Family and count_Multi_Family in each zip code:
I used this code but I don't know what to do next:
b=Combineddf[Combineddf['Property Type']=='Multi-Family'].count()
a=Combineddf[Combineddf['Property Type']=='Single Family'].count()
I can do this with sql but I keep trying to get it to work with pandas but no luck. Thank you for the help.
Edit*
Input:
import pandas as pd
input= pd.DataFrame({'Zip Code': [90001, 90001, 90001,90002,90002,90002],
'Total Males': [28468, 28468, 28468,43533,43533,43533],
'Total Female': [32135, 32135, 32135,54354,54354,54354],
'Property Type': ['Multi-Family', 'Multi-Family', 'Single Family','Single Family','Single Family','Multi-Family']
})
input
Desired output:
out = pd.DataFrame({'Zip Code': [90001, 90001, 90001,90002,90002,90002],
'Total Males': [28468, 28468, 28468,43533,43533,43533],
'Total Female': [32135, 32135, 32135,54354,54354,54354],
'Multi-Family': [2, 2, 2,1,1,1],
'Single Family': [1, 1, 1,2,2,2],
})
out
python pandas split pandas-groupby
1
Use groupby.count
– Ken Dekalb
Nov 25 '18 at 20:38
1
@PeterLeimbigler this is from csv file i fixed the link
– mango90001
Nov 25 '18 at 20:55
add a comment |
I have the following table:
https://ibb.co/DMGCgD2
I want to split (Property Type) to two column count_Single_Family and count_Multi_Family in each zip code:
I used this code but I don't know what to do next:
b=Combineddf[Combineddf['Property Type']=='Multi-Family'].count()
a=Combineddf[Combineddf['Property Type']=='Single Family'].count()
I can do this with sql but I keep trying to get it to work with pandas but no luck. Thank you for the help.
Edit*
Input:
import pandas as pd
input= pd.DataFrame({'Zip Code': [90001, 90001, 90001,90002,90002,90002],
'Total Males': [28468, 28468, 28468,43533,43533,43533],
'Total Female': [32135, 32135, 32135,54354,54354,54354],
'Property Type': ['Multi-Family', 'Multi-Family', 'Single Family','Single Family','Single Family','Multi-Family']
})
input
Desired output:
out = pd.DataFrame({'Zip Code': [90001, 90001, 90001,90002,90002,90002],
'Total Males': [28468, 28468, 28468,43533,43533,43533],
'Total Female': [32135, 32135, 32135,54354,54354,54354],
'Multi-Family': [2, 2, 2,1,1,1],
'Single Family': [1, 1, 1,2,2,2],
})
out
python pandas split pandas-groupby
I have the following table:
https://ibb.co/DMGCgD2
I want to split (Property Type) to two column count_Single_Family and count_Multi_Family in each zip code:
I used this code but I don't know what to do next:
b=Combineddf[Combineddf['Property Type']=='Multi-Family'].count()
a=Combineddf[Combineddf['Property Type']=='Single Family'].count()
I can do this with sql but I keep trying to get it to work with pandas but no luck. Thank you for the help.
Edit*
Input:
import pandas as pd
input= pd.DataFrame({'Zip Code': [90001, 90001, 90001,90002,90002,90002],
'Total Males': [28468, 28468, 28468,43533,43533,43533],
'Total Female': [32135, 32135, 32135,54354,54354,54354],
'Property Type': ['Multi-Family', 'Multi-Family', 'Single Family','Single Family','Single Family','Multi-Family']
})
input
Desired output:
out = pd.DataFrame({'Zip Code': [90001, 90001, 90001,90002,90002,90002],
'Total Males': [28468, 28468, 28468,43533,43533,43533],
'Total Female': [32135, 32135, 32135,54354,54354,54354],
'Multi-Family': [2, 2, 2,1,1,1],
'Single Family': [1, 1, 1,2,2,2],
})
out
python pandas split pandas-groupby
python pandas split pandas-groupby
edited Nov 26 '18 at 16:39
jpp
102k2165115
102k2165115
asked Nov 25 '18 at 20:31
mango90001mango90001
235
235
1
Use groupby.count
– Ken Dekalb
Nov 25 '18 at 20:38
1
@PeterLeimbigler this is from csv file i fixed the link
– mango90001
Nov 25 '18 at 20:55
add a comment |
1
Use groupby.count
– Ken Dekalb
Nov 25 '18 at 20:38
1
@PeterLeimbigler this is from csv file i fixed the link
– mango90001
Nov 25 '18 at 20:55
1
1
Use groupby.count
– Ken Dekalb
Nov 25 '18 at 20:38
Use groupby.count
– Ken Dekalb
Nov 25 '18 at 20:38
1
1
@PeterLeimbigler this is from csv file i fixed the link
– mango90001
Nov 25 '18 at 20:55
@PeterLeimbigler this is from csv file i fixed the link
– mango90001
Nov 25 '18 at 20:55
add a comment |
3 Answers
3
active
oldest
votes
pd.crosstab + merge
Pandas has a method to cross-tabulate based on an index and values. You then need only merge these results with your original dataframe.
df_cross = pd.crosstab(df['Zip Code'], df['Property Type'])
res = df.merge(df_cross, left_on='Zip Code', right_index=True)
print(res)
Zip Code Total Males Total Female Property Type Multi-Family
0 90001 28468 32135 Multi-Family 2
1 90001 28468 32135 Multi-Family 2
2 90001 28468 32135 Single Family 2
3 90002 43533 54354 Single Family 1
4 90002 43533 54354 Single Family 1
5 90002 43533 54354 Multi-Family 1
Single Family
0 1
1 1
2 1
3 2
4 2
5 2
add a comment |
This seems to yield the right solution:
v = input.groupby(['Zip Code','Property Type']).count().reset_index().pivot(index='Zip Code',columns = 'Property Type',values = 'Total Males')
pd.merge(input,v, how = 'inner', left_on = 'Zip Code', right_on ='Zip Code').drop('Property Type',axis = 1)
the merge code giving me this KeyError: 'Zip Code'
– mango90001
Nov 25 '18 at 21:55
Works here on your input code.
– Christian Sloper
Nov 25 '18 at 21:56
how does v.columns look for you?
– Christian Sloper
Nov 25 '18 at 21:58
add a comment |
Another solution:
(pd.concat([input.set_index('Zip Code'),
input.groupby('Zip Code')['Property Type']
.value_counts()
.unstack()], axis=1)
).reset_index()
Zip Code Total Males Total Female Property Type Multi-Family Single Family
0 90001 28468 32135 Multi-Family 2 1
1 90001 28468 32135 Multi-Family 2 1
2 90001 28468 32135 Single Family 2 1
3 90002 43533 54354 Single Family 1 2
4 90002 43533 54354 Single Family 1 2
5 90002 43533 54354 Multi-Family 1 2
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53471647%2fpandas-split-gender-column-to-two-columns%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
pd.crosstab + merge
Pandas has a method to cross-tabulate based on an index and values. You then need only merge these results with your original dataframe.
df_cross = pd.crosstab(df['Zip Code'], df['Property Type'])
res = df.merge(df_cross, left_on='Zip Code', right_index=True)
print(res)
Zip Code Total Males Total Female Property Type Multi-Family
0 90001 28468 32135 Multi-Family 2
1 90001 28468 32135 Multi-Family 2
2 90001 28468 32135 Single Family 2
3 90002 43533 54354 Single Family 1
4 90002 43533 54354 Single Family 1
5 90002 43533 54354 Multi-Family 1
Single Family
0 1
1 1
2 1
3 2
4 2
5 2
add a comment |
pd.crosstab + merge
Pandas has a method to cross-tabulate based on an index and values. You then need only merge these results with your original dataframe.
df_cross = pd.crosstab(df['Zip Code'], df['Property Type'])
res = df.merge(df_cross, left_on='Zip Code', right_index=True)
print(res)
Zip Code Total Males Total Female Property Type Multi-Family
0 90001 28468 32135 Multi-Family 2
1 90001 28468 32135 Multi-Family 2
2 90001 28468 32135 Single Family 2
3 90002 43533 54354 Single Family 1
4 90002 43533 54354 Single Family 1
5 90002 43533 54354 Multi-Family 1
Single Family
0 1
1 1
2 1
3 2
4 2
5 2
add a comment |
pd.crosstab + merge
Pandas has a method to cross-tabulate based on an index and values. You then need only merge these results with your original dataframe.
df_cross = pd.crosstab(df['Zip Code'], df['Property Type'])
res = df.merge(df_cross, left_on='Zip Code', right_index=True)
print(res)
Zip Code Total Males Total Female Property Type Multi-Family
0 90001 28468 32135 Multi-Family 2
1 90001 28468 32135 Multi-Family 2
2 90001 28468 32135 Single Family 2
3 90002 43533 54354 Single Family 1
4 90002 43533 54354 Single Family 1
5 90002 43533 54354 Multi-Family 1
Single Family
0 1
1 1
2 1
3 2
4 2
5 2
pd.crosstab + merge
Pandas has a method to cross-tabulate based on an index and values. You then need only merge these results with your original dataframe.
df_cross = pd.crosstab(df['Zip Code'], df['Property Type'])
res = df.merge(df_cross, left_on='Zip Code', right_index=True)
print(res)
Zip Code Total Males Total Female Property Type Multi-Family
0 90001 28468 32135 Multi-Family 2
1 90001 28468 32135 Multi-Family 2
2 90001 28468 32135 Single Family 2
3 90002 43533 54354 Single Family 1
4 90002 43533 54354 Single Family 1
5 90002 43533 54354 Multi-Family 1
Single Family
0 1
1 1
2 1
3 2
4 2
5 2
answered Nov 25 '18 at 22:02
jppjpp
102k2165115
102k2165115
add a comment |
add a comment |
This seems to yield the right solution:
v = input.groupby(['Zip Code','Property Type']).count().reset_index().pivot(index='Zip Code',columns = 'Property Type',values = 'Total Males')
pd.merge(input,v, how = 'inner', left_on = 'Zip Code', right_on ='Zip Code').drop('Property Type',axis = 1)
the merge code giving me this KeyError: 'Zip Code'
– mango90001
Nov 25 '18 at 21:55
Works here on your input code.
– Christian Sloper
Nov 25 '18 at 21:56
how does v.columns look for you?
– Christian Sloper
Nov 25 '18 at 21:58
add a comment |
This seems to yield the right solution:
v = input.groupby(['Zip Code','Property Type']).count().reset_index().pivot(index='Zip Code',columns = 'Property Type',values = 'Total Males')
pd.merge(input,v, how = 'inner', left_on = 'Zip Code', right_on ='Zip Code').drop('Property Type',axis = 1)
the merge code giving me this KeyError: 'Zip Code'
– mango90001
Nov 25 '18 at 21:55
Works here on your input code.
– Christian Sloper
Nov 25 '18 at 21:56
how does v.columns look for you?
– Christian Sloper
Nov 25 '18 at 21:58
add a comment |
This seems to yield the right solution:
v = input.groupby(['Zip Code','Property Type']).count().reset_index().pivot(index='Zip Code',columns = 'Property Type',values = 'Total Males')
pd.merge(input,v, how = 'inner', left_on = 'Zip Code', right_on ='Zip Code').drop('Property Type',axis = 1)
This seems to yield the right solution:
v = input.groupby(['Zip Code','Property Type']).count().reset_index().pivot(index='Zip Code',columns = 'Property Type',values = 'Total Males')
pd.merge(input,v, how = 'inner', left_on = 'Zip Code', right_on ='Zip Code').drop('Property Type',axis = 1)
answered Nov 25 '18 at 21:40
Christian SloperChristian Sloper
1,815416
1,815416
the merge code giving me this KeyError: 'Zip Code'
– mango90001
Nov 25 '18 at 21:55
Works here on your input code.
– Christian Sloper
Nov 25 '18 at 21:56
how does v.columns look for you?
– Christian Sloper
Nov 25 '18 at 21:58
add a comment |
the merge code giving me this KeyError: 'Zip Code'
– mango90001
Nov 25 '18 at 21:55
Works here on your input code.
– Christian Sloper
Nov 25 '18 at 21:56
how does v.columns look for you?
– Christian Sloper
Nov 25 '18 at 21:58
the merge code giving me this KeyError: 'Zip Code'
– mango90001
Nov 25 '18 at 21:55
the merge code giving me this KeyError: 'Zip Code'
– mango90001
Nov 25 '18 at 21:55
Works here on your input code.
– Christian Sloper
Nov 25 '18 at 21:56
Works here on your input code.
– Christian Sloper
Nov 25 '18 at 21:56
how does v.columns look for you?
– Christian Sloper
Nov 25 '18 at 21:58
how does v.columns look for you?
– Christian Sloper
Nov 25 '18 at 21:58
add a comment |
Another solution:
(pd.concat([input.set_index('Zip Code'),
input.groupby('Zip Code')['Property Type']
.value_counts()
.unstack()], axis=1)
).reset_index()
Zip Code Total Males Total Female Property Type Multi-Family Single Family
0 90001 28468 32135 Multi-Family 2 1
1 90001 28468 32135 Multi-Family 2 1
2 90001 28468 32135 Single Family 2 1
3 90002 43533 54354 Single Family 1 2
4 90002 43533 54354 Single Family 1 2
5 90002 43533 54354 Multi-Family 1 2
add a comment |
Another solution:
(pd.concat([input.set_index('Zip Code'),
input.groupby('Zip Code')['Property Type']
.value_counts()
.unstack()], axis=1)
).reset_index()
Zip Code Total Males Total Female Property Type Multi-Family Single Family
0 90001 28468 32135 Multi-Family 2 1
1 90001 28468 32135 Multi-Family 2 1
2 90001 28468 32135 Single Family 2 1
3 90002 43533 54354 Single Family 1 2
4 90002 43533 54354 Single Family 1 2
5 90002 43533 54354 Multi-Family 1 2
add a comment |
Another solution:
(pd.concat([input.set_index('Zip Code'),
input.groupby('Zip Code')['Property Type']
.value_counts()
.unstack()], axis=1)
).reset_index()
Zip Code Total Males Total Female Property Type Multi-Family Single Family
0 90001 28468 32135 Multi-Family 2 1
1 90001 28468 32135 Multi-Family 2 1
2 90001 28468 32135 Single Family 2 1
3 90002 43533 54354 Single Family 1 2
4 90002 43533 54354 Single Family 1 2
5 90002 43533 54354 Multi-Family 1 2
Another solution:
(pd.concat([input.set_index('Zip Code'),
input.groupby('Zip Code')['Property Type']
.value_counts()
.unstack()], axis=1)
).reset_index()
Zip Code Total Males Total Female Property Type Multi-Family Single Family
0 90001 28468 32135 Multi-Family 2 1
1 90001 28468 32135 Multi-Family 2 1
2 90001 28468 32135 Single Family 2 1
3 90002 43533 54354 Single Family 1 2
4 90002 43533 54354 Single Family 1 2
5 90002 43533 54354 Multi-Family 1 2
edited Nov 25 '18 at 21:51
answered Nov 25 '18 at 21:41
Peter LeimbiglerPeter Leimbigler
4,6631416
4,6631416
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53471647%2fpandas-split-gender-column-to-two-columns%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Use groupby.count
– Ken Dekalb
Nov 25 '18 at 20:38
1
@PeterLeimbigler this is from csv file i fixed the link
– mango90001
Nov 25 '18 at 20:55