How to give column names after count and joins?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have the following dataframe
import pandas as pd
compnaies = ['Microsoft', 'Google', 'Amazon', 'Microsoft', 'Facebook', 'Google','Google']
products = ['OS', 'Search', 'E-comm', 'X-box', 'Social Media', 'Android','Search']
df = pd.DataFrame({'company' : compnaies, 'product':products })
I do the following operation:
df.groupby('company').product.agg([('count', 'count'), ('product', ', '.join)])
count product
company
Amazon 1 E-comm
Facebook 1 Social Media
Google 3 Search, Android, Search
Microsoft 2 OS, X-box
How to name the columns after the above code instead of count and product:
Expected Output:
company Number Product List.
Amazon 1 E-comm
Facebook 1 Social Media
Google 3 Search, Android, Search
Microsoft 2 OS, X-box
Expected Output 2:
Expected Output:
company Number Product List. uniquecount uniquevalues
Amazon 1 E-comm 1 E-comm
Facebook 1 Social Media 2 Social Media
Google 3 Search, Android, Search 2 Search, Android,
Microsoft 2 OS, X-box, Search 3 OS, X-box,Search
python pandas
add a comment |
I have the following dataframe
import pandas as pd
compnaies = ['Microsoft', 'Google', 'Amazon', 'Microsoft', 'Facebook', 'Google','Google']
products = ['OS', 'Search', 'E-comm', 'X-box', 'Social Media', 'Android','Search']
df = pd.DataFrame({'company' : compnaies, 'product':products })
I do the following operation:
df.groupby('company').product.agg([('count', 'count'), ('product', ', '.join)])
count product
company
Amazon 1 E-comm
Facebook 1 Social Media
Google 3 Search, Android, Search
Microsoft 2 OS, X-box
How to name the columns after the above code instead of count and product:
Expected Output:
company Number Product List.
Amazon 1 E-comm
Facebook 1 Social Media
Google 3 Search, Android, Search
Microsoft 2 OS, X-box
Expected Output 2:
Expected Output:
company Number Product List. uniquecount uniquevalues
Amazon 1 E-comm 1 E-comm
Facebook 1 Social Media 2 Social Media
Google 3 Search, Android, Search 2 Search, Android,
Microsoft 2 OS, X-box, Search 3 OS, X-box,Search
python pandas
final_df.reset_index()
and rename the columns
– Madhuri
Jan 3 at 5:11
@Madhuri I tried but it doesn't work I need to rename two columns?
– panda
Jan 3 at 5:14
add a comment |
I have the following dataframe
import pandas as pd
compnaies = ['Microsoft', 'Google', 'Amazon', 'Microsoft', 'Facebook', 'Google','Google']
products = ['OS', 'Search', 'E-comm', 'X-box', 'Social Media', 'Android','Search']
df = pd.DataFrame({'company' : compnaies, 'product':products })
I do the following operation:
df.groupby('company').product.agg([('count', 'count'), ('product', ', '.join)])
count product
company
Amazon 1 E-comm
Facebook 1 Social Media
Google 3 Search, Android, Search
Microsoft 2 OS, X-box
How to name the columns after the above code instead of count and product:
Expected Output:
company Number Product List.
Amazon 1 E-comm
Facebook 1 Social Media
Google 3 Search, Android, Search
Microsoft 2 OS, X-box
Expected Output 2:
Expected Output:
company Number Product List. uniquecount uniquevalues
Amazon 1 E-comm 1 E-comm
Facebook 1 Social Media 2 Social Media
Google 3 Search, Android, Search 2 Search, Android,
Microsoft 2 OS, X-box, Search 3 OS, X-box,Search
python pandas
I have the following dataframe
import pandas as pd
compnaies = ['Microsoft', 'Google', 'Amazon', 'Microsoft', 'Facebook', 'Google','Google']
products = ['OS', 'Search', 'E-comm', 'X-box', 'Social Media', 'Android','Search']
df = pd.DataFrame({'company' : compnaies, 'product':products })
I do the following operation:
df.groupby('company').product.agg([('count', 'count'), ('product', ', '.join)])
count product
company
Amazon 1 E-comm
Facebook 1 Social Media
Google 3 Search, Android, Search
Microsoft 2 OS, X-box
How to name the columns after the above code instead of count and product:
Expected Output:
company Number Product List.
Amazon 1 E-comm
Facebook 1 Social Media
Google 3 Search, Android, Search
Microsoft 2 OS, X-box
Expected Output 2:
Expected Output:
company Number Product List. uniquecount uniquevalues
Amazon 1 E-comm 1 E-comm
Facebook 1 Social Media 2 Social Media
Google 3 Search, Android, Search 2 Search, Android,
Microsoft 2 OS, X-box, Search 3 OS, X-box,Search
python pandas
python pandas
edited Jan 3 at 5:36
panda
asked Jan 3 at 5:08
pandapanda
1608
1608
final_df.reset_index()
and rename the columns
– Madhuri
Jan 3 at 5:11
@Madhuri I tried but it doesn't work I need to rename two columns?
– panda
Jan 3 at 5:14
add a comment |
final_df.reset_index()
and rename the columns
– Madhuri
Jan 3 at 5:11
@Madhuri I tried but it doesn't work I need to rename two columns?
– panda
Jan 3 at 5:14
final_df.reset_index()
and rename the columns– Madhuri
Jan 3 at 5:11
final_df.reset_index()
and rename the columns– Madhuri
Jan 3 at 5:11
@Madhuri I tried but it doesn't work I need to rename two columns?
– panda
Jan 3 at 5:14
@Madhuri I tried but it doesn't work I need to rename two columns?
– panda
Jan 3 at 5:14
add a comment |
2 Answers
2
active
oldest
votes
import pandas as pd
def remove_dup(string):
temp=string.split(',')
temp=[x.strip() for x in temp]
return ','.join(set(temp))
compnaies = ['Microsoft', 'Google', 'Amazon', 'Microsoft', 'Facebook', 'Google','Google']
products = ['OS', 'Search', 'E-comm', 'X-box', 'Social Media', 'Android','Search']
df = pd.DataFrame({'company' : compnaies, 'product':products })
new_df=df.groupby('company').product.agg([('Number', 'count'), ('Product list', ', '.join)]).reset_index()
#create uniquevalues
new_df['uniquevalues']=new_df['Product list'].apply(remove_dup)
#create uniquecount
new_df['uniquecount']=new_df['uniquevalues'].str.split(',').str.len()
it works How to count the unique values and also print the unique values on the product list can you help?
– panda
Jan 3 at 5:18
what unique values are you referring to ? , you want to remove duplicate values indf['Product List']
?
– pyd
Jan 3 at 5:21
yes I mean that and generate a new column
– panda
Jan 3 at 5:22
I just added it as Expected output 2
– panda
Jan 3 at 5:25
1
thank u so much, it works
– panda
Jan 3 at 5:47
|
show 1 more comment
Here is the standard answer:
df.groupby('company').product.agg([('count', 'count'), ('product', ', '.join)]).rename(columns={"count":"number","product":"product lists"})
Pandas Online help
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%2f54016652%2fhow-to-give-column-names-after-count-and-joins%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
import pandas as pd
def remove_dup(string):
temp=string.split(',')
temp=[x.strip() for x in temp]
return ','.join(set(temp))
compnaies = ['Microsoft', 'Google', 'Amazon', 'Microsoft', 'Facebook', 'Google','Google']
products = ['OS', 'Search', 'E-comm', 'X-box', 'Social Media', 'Android','Search']
df = pd.DataFrame({'company' : compnaies, 'product':products })
new_df=df.groupby('company').product.agg([('Number', 'count'), ('Product list', ', '.join)]).reset_index()
#create uniquevalues
new_df['uniquevalues']=new_df['Product list'].apply(remove_dup)
#create uniquecount
new_df['uniquecount']=new_df['uniquevalues'].str.split(',').str.len()
it works How to count the unique values and also print the unique values on the product list can you help?
– panda
Jan 3 at 5:18
what unique values are you referring to ? , you want to remove duplicate values indf['Product List']
?
– pyd
Jan 3 at 5:21
yes I mean that and generate a new column
– panda
Jan 3 at 5:22
I just added it as Expected output 2
– panda
Jan 3 at 5:25
1
thank u so much, it works
– panda
Jan 3 at 5:47
|
show 1 more comment
import pandas as pd
def remove_dup(string):
temp=string.split(',')
temp=[x.strip() for x in temp]
return ','.join(set(temp))
compnaies = ['Microsoft', 'Google', 'Amazon', 'Microsoft', 'Facebook', 'Google','Google']
products = ['OS', 'Search', 'E-comm', 'X-box', 'Social Media', 'Android','Search']
df = pd.DataFrame({'company' : compnaies, 'product':products })
new_df=df.groupby('company').product.agg([('Number', 'count'), ('Product list', ', '.join)]).reset_index()
#create uniquevalues
new_df['uniquevalues']=new_df['Product list'].apply(remove_dup)
#create uniquecount
new_df['uniquecount']=new_df['uniquevalues'].str.split(',').str.len()
it works How to count the unique values and also print the unique values on the product list can you help?
– panda
Jan 3 at 5:18
what unique values are you referring to ? , you want to remove duplicate values indf['Product List']
?
– pyd
Jan 3 at 5:21
yes I mean that and generate a new column
– panda
Jan 3 at 5:22
I just added it as Expected output 2
– panda
Jan 3 at 5:25
1
thank u so much, it works
– panda
Jan 3 at 5:47
|
show 1 more comment
import pandas as pd
def remove_dup(string):
temp=string.split(',')
temp=[x.strip() for x in temp]
return ','.join(set(temp))
compnaies = ['Microsoft', 'Google', 'Amazon', 'Microsoft', 'Facebook', 'Google','Google']
products = ['OS', 'Search', 'E-comm', 'X-box', 'Social Media', 'Android','Search']
df = pd.DataFrame({'company' : compnaies, 'product':products })
new_df=df.groupby('company').product.agg([('Number', 'count'), ('Product list', ', '.join)]).reset_index()
#create uniquevalues
new_df['uniquevalues']=new_df['Product list'].apply(remove_dup)
#create uniquecount
new_df['uniquecount']=new_df['uniquevalues'].str.split(',').str.len()
import pandas as pd
def remove_dup(string):
temp=string.split(',')
temp=[x.strip() for x in temp]
return ','.join(set(temp))
compnaies = ['Microsoft', 'Google', 'Amazon', 'Microsoft', 'Facebook', 'Google','Google']
products = ['OS', 'Search', 'E-comm', 'X-box', 'Social Media', 'Android','Search']
df = pd.DataFrame({'company' : compnaies, 'product':products })
new_df=df.groupby('company').product.agg([('Number', 'count'), ('Product list', ', '.join)]).reset_index()
#create uniquevalues
new_df['uniquevalues']=new_df['Product list'].apply(remove_dup)
#create uniquecount
new_df['uniquecount']=new_df['uniquevalues'].str.split(',').str.len()
edited Jan 3 at 5:43
answered Jan 3 at 5:15


pydpyd
2,14211229
2,14211229
it works How to count the unique values and also print the unique values on the product list can you help?
– panda
Jan 3 at 5:18
what unique values are you referring to ? , you want to remove duplicate values indf['Product List']
?
– pyd
Jan 3 at 5:21
yes I mean that and generate a new column
– panda
Jan 3 at 5:22
I just added it as Expected output 2
– panda
Jan 3 at 5:25
1
thank u so much, it works
– panda
Jan 3 at 5:47
|
show 1 more comment
it works How to count the unique values and also print the unique values on the product list can you help?
– panda
Jan 3 at 5:18
what unique values are you referring to ? , you want to remove duplicate values indf['Product List']
?
– pyd
Jan 3 at 5:21
yes I mean that and generate a new column
– panda
Jan 3 at 5:22
I just added it as Expected output 2
– panda
Jan 3 at 5:25
1
thank u so much, it works
– panda
Jan 3 at 5:47
it works How to count the unique values and also print the unique values on the product list can you help?
– panda
Jan 3 at 5:18
it works How to count the unique values and also print the unique values on the product list can you help?
– panda
Jan 3 at 5:18
what unique values are you referring to ? , you want to remove duplicate values in
df['Product List']
?– pyd
Jan 3 at 5:21
what unique values are you referring to ? , you want to remove duplicate values in
df['Product List']
?– pyd
Jan 3 at 5:21
yes I mean that and generate a new column
– panda
Jan 3 at 5:22
yes I mean that and generate a new column
– panda
Jan 3 at 5:22
I just added it as Expected output 2
– panda
Jan 3 at 5:25
I just added it as Expected output 2
– panda
Jan 3 at 5:25
1
1
thank u so much, it works
– panda
Jan 3 at 5:47
thank u so much, it works
– panda
Jan 3 at 5:47
|
show 1 more comment
Here is the standard answer:
df.groupby('company').product.agg([('count', 'count'), ('product', ', '.join)]).rename(columns={"count":"number","product":"product lists"})
Pandas Online help
add a comment |
Here is the standard answer:
df.groupby('company').product.agg([('count', 'count'), ('product', ', '.join)]).rename(columns={"count":"number","product":"product lists"})
Pandas Online help
add a comment |
Here is the standard answer:
df.groupby('company').product.agg([('count', 'count'), ('product', ', '.join)]).rename(columns={"count":"number","product":"product lists"})
Pandas Online help
Here is the standard answer:
df.groupby('company').product.agg([('count', 'count'), ('product', ', '.join)]).rename(columns={"count":"number","product":"product lists"})
Pandas Online help
edited Jan 3 at 5:40


pyd
2,14211229
2,14211229
answered Jan 3 at 5:26


Yong WangYong Wang
4613
4613
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%2f54016652%2fhow-to-give-column-names-after-count-and-joins%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
final_df.reset_index()
and rename the columns– Madhuri
Jan 3 at 5:11
@Madhuri I tried but it doesn't work I need to rename two columns?
– panda
Jan 3 at 5:14