groupby and function call in pandas
up vote
4
down vote
favorite
I have a dataframe where i have a column "Name". Name have multiples values like sample1, sample2, sample3. I want to apply a function to all those groups where value in Name column is same.
Output:
Name Value Result
0 Name1 2 5
1 Name1 3 5
2 Name2 1 11
3 Name2 4 11
4 Name2 6 11
5 Name3 8 10
6 Name3 2 10
python python-3.x pandas
add a comment |
up vote
4
down vote
favorite
I have a dataframe where i have a column "Name". Name have multiples values like sample1, sample2, sample3. I want to apply a function to all those groups where value in Name column is same.
Output:
Name Value Result
0 Name1 2 5
1 Name1 3 5
2 Name2 1 11
3 Name2 4 11
4 Name2 6 11
5 Name3 8 10
6 Name3 2 10
python python-3.x pandas
Have you looked at the Pandasgroupby
function? pandas.pydata.org/pandas-docs/stable/groupby.html
– DatHydroGuy
2 days ago
add a comment |
up vote
4
down vote
favorite
up vote
4
down vote
favorite
I have a dataframe where i have a column "Name". Name have multiples values like sample1, sample2, sample3. I want to apply a function to all those groups where value in Name column is same.
Output:
Name Value Result
0 Name1 2 5
1 Name1 3 5
2 Name2 1 11
3 Name2 4 11
4 Name2 6 11
5 Name3 8 10
6 Name3 2 10
python python-3.x pandas
I have a dataframe where i have a column "Name". Name have multiples values like sample1, sample2, sample3. I want to apply a function to all those groups where value in Name column is same.
Output:
Name Value Result
0 Name1 2 5
1 Name1 3 5
2 Name2 1 11
3 Name2 4 11
4 Name2 6 11
5 Name3 8 10
6 Name3 2 10
python python-3.x pandas
python python-3.x pandas
edited 2 days ago
nixon
83116
83116
asked 2 days ago
Chetan P
477
477
Have you looked at the Pandasgroupby
function? pandas.pydata.org/pandas-docs/stable/groupby.html
– DatHydroGuy
2 days ago
add a comment |
Have you looked at the Pandasgroupby
function? pandas.pydata.org/pandas-docs/stable/groupby.html
– DatHydroGuy
2 days ago
Have you looked at the Pandas
groupby
function? pandas.pydata.org/pandas-docs/stable/groupby.html– DatHydroGuy
2 days ago
Have you looked at the Pandas
groupby
function? pandas.pydata.org/pandas-docs/stable/groupby.html– DatHydroGuy
2 days ago
add a comment |
3 Answers
3
active
oldest
votes
up vote
2
down vote
accepted
Looks like you want a groupby.apply
. Something like this should work:
import pandas as pd
df = # ... load your data
def group_sum(g):
g["Result"] = g["Value"].sum()
return g
df_grouped = df.groupby("Name").apply(group_sum)
Edit: Alexandre Nixon's answer is better for this use case.
New contributor
add a comment |
up vote
0
down vote
You want to use transform
for that, which returns a dataframe with the transformed values and the same dimension.
a['Result'] = a.groupby('Name').transform(lambda x: x.sum())
For example if you have:
df = pd.DataFrame({'Name':['Name2','Name2', 'Name3'],
'Value':[1,2,4]},
columns = ['Name','Value'])
df['Result'] = df.groupby('Name').transform(lambda x: x.sum())
print(df)
Name Value Result
0 Name2 1 3
1 Name2 2 3
2 Name3 4 4
add a comment |
up vote
0
down vote
Df.groupby('Name').apply(lambda x: function (x.value))
The will work, in x.value you can put your column name
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Looks like you want a groupby.apply
. Something like this should work:
import pandas as pd
df = # ... load your data
def group_sum(g):
g["Result"] = g["Value"].sum()
return g
df_grouped = df.groupby("Name").apply(group_sum)
Edit: Alexandre Nixon's answer is better for this use case.
New contributor
add a comment |
up vote
2
down vote
accepted
Looks like you want a groupby.apply
. Something like this should work:
import pandas as pd
df = # ... load your data
def group_sum(g):
g["Result"] = g["Value"].sum()
return g
df_grouped = df.groupby("Name").apply(group_sum)
Edit: Alexandre Nixon's answer is better for this use case.
New contributor
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Looks like you want a groupby.apply
. Something like this should work:
import pandas as pd
df = # ... load your data
def group_sum(g):
g["Result"] = g["Value"].sum()
return g
df_grouped = df.groupby("Name").apply(group_sum)
Edit: Alexandre Nixon's answer is better for this use case.
New contributor
Looks like you want a groupby.apply
. Something like this should work:
import pandas as pd
df = # ... load your data
def group_sum(g):
g["Result"] = g["Value"].sum()
return g
df_grouped = df.groupby("Name").apply(group_sum)
Edit: Alexandre Nixon's answer is better for this use case.
New contributor
edited 2 days ago
New contributor
answered 2 days ago
johnpaton
23516
23516
New contributor
New contributor
add a comment |
add a comment |
up vote
0
down vote
You want to use transform
for that, which returns a dataframe with the transformed values and the same dimension.
a['Result'] = a.groupby('Name').transform(lambda x: x.sum())
For example if you have:
df = pd.DataFrame({'Name':['Name2','Name2', 'Name3'],
'Value':[1,2,4]},
columns = ['Name','Value'])
df['Result'] = df.groupby('Name').transform(lambda x: x.sum())
print(df)
Name Value Result
0 Name2 1 3
1 Name2 2 3
2 Name3 4 4
add a comment |
up vote
0
down vote
You want to use transform
for that, which returns a dataframe with the transformed values and the same dimension.
a['Result'] = a.groupby('Name').transform(lambda x: x.sum())
For example if you have:
df = pd.DataFrame({'Name':['Name2','Name2', 'Name3'],
'Value':[1,2,4]},
columns = ['Name','Value'])
df['Result'] = df.groupby('Name').transform(lambda x: x.sum())
print(df)
Name Value Result
0 Name2 1 3
1 Name2 2 3
2 Name3 4 4
add a comment |
up vote
0
down vote
up vote
0
down vote
You want to use transform
for that, which returns a dataframe with the transformed values and the same dimension.
a['Result'] = a.groupby('Name').transform(lambda x: x.sum())
For example if you have:
df = pd.DataFrame({'Name':['Name2','Name2', 'Name3'],
'Value':[1,2,4]},
columns = ['Name','Value'])
df['Result'] = df.groupby('Name').transform(lambda x: x.sum())
print(df)
Name Value Result
0 Name2 1 3
1 Name2 2 3
2 Name3 4 4
You want to use transform
for that, which returns a dataframe with the transformed values and the same dimension.
a['Result'] = a.groupby('Name').transform(lambda x: x.sum())
For example if you have:
df = pd.DataFrame({'Name':['Name2','Name2', 'Name3'],
'Value':[1,2,4]},
columns = ['Name','Value'])
df['Result'] = df.groupby('Name').transform(lambda x: x.sum())
print(df)
Name Value Result
0 Name2 1 3
1 Name2 2 3
2 Name3 4 4
edited 2 days ago
answered 2 days ago
nixon
83116
83116
add a comment |
add a comment |
up vote
0
down vote
Df.groupby('Name').apply(lambda x: function (x.value))
The will work, in x.value you can put your column name
add a comment |
up vote
0
down vote
Df.groupby('Name').apply(lambda x: function (x.value))
The will work, in x.value you can put your column name
add a comment |
up vote
0
down vote
up vote
0
down vote
Df.groupby('Name').apply(lambda x: function (x.value))
The will work, in x.value you can put your column name
Df.groupby('Name').apply(lambda x: function (x.value))
The will work, in x.value you can put your column name
answered 2 days ago
id101112
167115
167115
add a comment |
add a comment |
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%2f53410178%2fgroupby-and-function-call-in-pandas%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
Have you looked at the Pandas
groupby
function? pandas.pydata.org/pandas-docs/stable/groupby.html– DatHydroGuy
2 days ago