Combine SIMILAR value rows using Python Pandas
Suppose I have the following Dataframe-
company money
jack & jill, Boston, MA 02215 51
jack & jill, MA 02215 49
Now, I know that these 2 rows mean the same company, so I want to merge them and also sum the money-
company money
jack & jill, Boston, MA 02215 100
I don't care about the format of the company name, as long as the duplicates get merged and the money gets added.
How should I go about this? Is there a library out there that merges SIMILAR value rows and sums the corresponding quantitative value?
python-3.x pandas dataframe data-science
add a comment |
Suppose I have the following Dataframe-
company money
jack & jill, Boston, MA 02215 51
jack & jill, MA 02215 49
Now, I know that these 2 rows mean the same company, so I want to merge them and also sum the money-
company money
jack & jill, Boston, MA 02215 100
I don't care about the format of the company name, as long as the duplicates get merged and the money gets added.
How should I go about this? Is there a library out there that merges SIMILAR value rows and sums the corresponding quantitative value?
python-3.x pandas dataframe data-science
1
Have a look at fuzzywuzzy: github.com/seatgeek/fuzzywuzzy
– Peter Leimbigler
Nov 16 '18 at 4:52
try usingcompany.startswith('jack & jill')
and then groupby using the company column.
– Ananth Reddy
Nov 16 '18 at 6:21
@AnanthReddy This is just an example. There are 1000's of rows with multiple company names.
– kev
Nov 19 '18 at 18:35
@PeterLeimbigler Thanks for the suggestion! Although, how do you reckon I use it with a CSV file where there can be, for example, 4 rows with similar company names? How do iterate my CSV file? Edit: I found this jonathansoma.com/lede/algorithms-2017/classes/… which might be useful.
– kev
Nov 19 '18 at 18:42
add a comment |
Suppose I have the following Dataframe-
company money
jack & jill, Boston, MA 02215 51
jack & jill, MA 02215 49
Now, I know that these 2 rows mean the same company, so I want to merge them and also sum the money-
company money
jack & jill, Boston, MA 02215 100
I don't care about the format of the company name, as long as the duplicates get merged and the money gets added.
How should I go about this? Is there a library out there that merges SIMILAR value rows and sums the corresponding quantitative value?
python-3.x pandas dataframe data-science
Suppose I have the following Dataframe-
company money
jack & jill, Boston, MA 02215 51
jack & jill, MA 02215 49
Now, I know that these 2 rows mean the same company, so I want to merge them and also sum the money-
company money
jack & jill, Boston, MA 02215 100
I don't care about the format of the company name, as long as the duplicates get merged and the money gets added.
How should I go about this? Is there a library out there that merges SIMILAR value rows and sums the corresponding quantitative value?
python-3.x pandas dataframe data-science
python-3.x pandas dataframe data-science
edited Nov 16 '18 at 4:49
Peter Leimbigler
3,8331415
3,8331415
asked Nov 16 '18 at 1:58
kevkev
79319
79319
1
Have a look at fuzzywuzzy: github.com/seatgeek/fuzzywuzzy
– Peter Leimbigler
Nov 16 '18 at 4:52
try usingcompany.startswith('jack & jill')
and then groupby using the company column.
– Ananth Reddy
Nov 16 '18 at 6:21
@AnanthReddy This is just an example. There are 1000's of rows with multiple company names.
– kev
Nov 19 '18 at 18:35
@PeterLeimbigler Thanks for the suggestion! Although, how do you reckon I use it with a CSV file where there can be, for example, 4 rows with similar company names? How do iterate my CSV file? Edit: I found this jonathansoma.com/lede/algorithms-2017/classes/… which might be useful.
– kev
Nov 19 '18 at 18:42
add a comment |
1
Have a look at fuzzywuzzy: github.com/seatgeek/fuzzywuzzy
– Peter Leimbigler
Nov 16 '18 at 4:52
try usingcompany.startswith('jack & jill')
and then groupby using the company column.
– Ananth Reddy
Nov 16 '18 at 6:21
@AnanthReddy This is just an example. There are 1000's of rows with multiple company names.
– kev
Nov 19 '18 at 18:35
@PeterLeimbigler Thanks for the suggestion! Although, how do you reckon I use it with a CSV file where there can be, for example, 4 rows with similar company names? How do iterate my CSV file? Edit: I found this jonathansoma.com/lede/algorithms-2017/classes/… which might be useful.
– kev
Nov 19 '18 at 18:42
1
1
Have a look at fuzzywuzzy: github.com/seatgeek/fuzzywuzzy
– Peter Leimbigler
Nov 16 '18 at 4:52
Have a look at fuzzywuzzy: github.com/seatgeek/fuzzywuzzy
– Peter Leimbigler
Nov 16 '18 at 4:52
try using
company.startswith('jack & jill')
and then groupby using the company column.– Ananth Reddy
Nov 16 '18 at 6:21
try using
company.startswith('jack & jill')
and then groupby using the company column.– Ananth Reddy
Nov 16 '18 at 6:21
@AnanthReddy This is just an example. There are 1000's of rows with multiple company names.
– kev
Nov 19 '18 at 18:35
@AnanthReddy This is just an example. There are 1000's of rows with multiple company names.
– kev
Nov 19 '18 at 18:35
@PeterLeimbigler Thanks for the suggestion! Although, how do you reckon I use it with a CSV file where there can be, for example, 4 rows with similar company names? How do iterate my CSV file? Edit: I found this jonathansoma.com/lede/algorithms-2017/classes/… which might be useful.
– kev
Nov 19 '18 at 18:42
@PeterLeimbigler Thanks for the suggestion! Although, how do you reckon I use it with a CSV file where there can be, for example, 4 rows with similar company names? How do iterate my CSV file? Edit: I found this jonathansoma.com/lede/algorithms-2017/classes/… which might be useful.
– kev
Nov 19 '18 at 18:42
add a comment |
1 Answer
1
active
oldest
votes
If you have same pattern in company
column i.e. the value before the 1st comma is company name. You can use something like below:
df = pd.DataFrame({'company':['jack & jill, Boston, MA 02215','jack & jill, MA 02215','Google, New Jersey', 'Google'],
'money':[51,49, 33, 22]})
df['company'] = df['company'].apply(lambda x: x.split(",")[0])
new_df = df.groupby(['company'])['money'].sum().reset_index()
print(new_df)
Output:
company money
0 Google 55
1 jack & jill 100
I'm afraid I don't have the same pattern in all the company names. Hence, I wanna look at something that helps me calculate the similarity between 2 strings. I think something like fuzzywuzzy might help me out. But I'm not sure if I can use it on a CSV file with 1000's of rows. Edit: I found this jonathansoma.com/lede/algorithms-2017/classes/… which might be useful.
– kev
Nov 19 '18 at 18:43
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%2f53330407%2fcombine-similar-value-rows-using-python-pandas%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you have same pattern in company
column i.e. the value before the 1st comma is company name. You can use something like below:
df = pd.DataFrame({'company':['jack & jill, Boston, MA 02215','jack & jill, MA 02215','Google, New Jersey', 'Google'],
'money':[51,49, 33, 22]})
df['company'] = df['company'].apply(lambda x: x.split(",")[0])
new_df = df.groupby(['company'])['money'].sum().reset_index()
print(new_df)
Output:
company money
0 Google 55
1 jack & jill 100
I'm afraid I don't have the same pattern in all the company names. Hence, I wanna look at something that helps me calculate the similarity between 2 strings. I think something like fuzzywuzzy might help me out. But I'm not sure if I can use it on a CSV file with 1000's of rows. Edit: I found this jonathansoma.com/lede/algorithms-2017/classes/… which might be useful.
– kev
Nov 19 '18 at 18:43
add a comment |
If you have same pattern in company
column i.e. the value before the 1st comma is company name. You can use something like below:
df = pd.DataFrame({'company':['jack & jill, Boston, MA 02215','jack & jill, MA 02215','Google, New Jersey', 'Google'],
'money':[51,49, 33, 22]})
df['company'] = df['company'].apply(lambda x: x.split(",")[0])
new_df = df.groupby(['company'])['money'].sum().reset_index()
print(new_df)
Output:
company money
0 Google 55
1 jack & jill 100
I'm afraid I don't have the same pattern in all the company names. Hence, I wanna look at something that helps me calculate the similarity between 2 strings. I think something like fuzzywuzzy might help me out. But I'm not sure if I can use it on a CSV file with 1000's of rows. Edit: I found this jonathansoma.com/lede/algorithms-2017/classes/… which might be useful.
– kev
Nov 19 '18 at 18:43
add a comment |
If you have same pattern in company
column i.e. the value before the 1st comma is company name. You can use something like below:
df = pd.DataFrame({'company':['jack & jill, Boston, MA 02215','jack & jill, MA 02215','Google, New Jersey', 'Google'],
'money':[51,49, 33, 22]})
df['company'] = df['company'].apply(lambda x: x.split(",")[0])
new_df = df.groupby(['company'])['money'].sum().reset_index()
print(new_df)
Output:
company money
0 Google 55
1 jack & jill 100
If you have same pattern in company
column i.e. the value before the 1st comma is company name. You can use something like below:
df = pd.DataFrame({'company':['jack & jill, Boston, MA 02215','jack & jill, MA 02215','Google, New Jersey', 'Google'],
'money':[51,49, 33, 22]})
df['company'] = df['company'].apply(lambda x: x.split(",")[0])
new_df = df.groupby(['company'])['money'].sum().reset_index()
print(new_df)
Output:
company money
0 Google 55
1 jack & jill 100
answered Nov 16 '18 at 5:07
SociopathSociopath
3,64981635
3,64981635
I'm afraid I don't have the same pattern in all the company names. Hence, I wanna look at something that helps me calculate the similarity between 2 strings. I think something like fuzzywuzzy might help me out. But I'm not sure if I can use it on a CSV file with 1000's of rows. Edit: I found this jonathansoma.com/lede/algorithms-2017/classes/… which might be useful.
– kev
Nov 19 '18 at 18:43
add a comment |
I'm afraid I don't have the same pattern in all the company names. Hence, I wanna look at something that helps me calculate the similarity between 2 strings. I think something like fuzzywuzzy might help me out. But I'm not sure if I can use it on a CSV file with 1000's of rows. Edit: I found this jonathansoma.com/lede/algorithms-2017/classes/… which might be useful.
– kev
Nov 19 '18 at 18:43
I'm afraid I don't have the same pattern in all the company names. Hence, I wanna look at something that helps me calculate the similarity between 2 strings. I think something like fuzzywuzzy might help me out. But I'm not sure if I can use it on a CSV file with 1000's of rows. Edit: I found this jonathansoma.com/lede/algorithms-2017/classes/… which might be useful.
– kev
Nov 19 '18 at 18:43
I'm afraid I don't have the same pattern in all the company names. Hence, I wanna look at something that helps me calculate the similarity between 2 strings. I think something like fuzzywuzzy might help me out. But I'm not sure if I can use it on a CSV file with 1000's of rows. Edit: I found this jonathansoma.com/lede/algorithms-2017/classes/… which might be useful.
– kev
Nov 19 '18 at 18:43
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%2f53330407%2fcombine-similar-value-rows-using-python-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
1
Have a look at fuzzywuzzy: github.com/seatgeek/fuzzywuzzy
– Peter Leimbigler
Nov 16 '18 at 4:52
try using
company.startswith('jack & jill')
and then groupby using the company column.– Ananth Reddy
Nov 16 '18 at 6:21
@AnanthReddy This is just an example. There are 1000's of rows with multiple company names.
– kev
Nov 19 '18 at 18:35
@PeterLeimbigler Thanks for the suggestion! Although, how do you reckon I use it with a CSV file where there can be, for example, 4 rows with similar company names? How do iterate my CSV file? Edit: I found this jonathansoma.com/lede/algorithms-2017/classes/… which might be useful.
– kev
Nov 19 '18 at 18:42