Get a count of combinations and its reverse from two columnsHow to get count of two-way combinations from two columns?How to return multiple values from a function?Speed comparison with Project Euler: C vs Python vs Erlang vs HaskellPandas: create two new columns in a dataframe with values calculated from a pre-existing columnDelete column from pandas DataFrame by column name“Large data” work flows using pandasSelect rows from a DataFrame based on values in a column in pandasPandas DataFrame Groupby two columns and get countsGet list from pandas DataFrame column headerspandas create new column based on values from other columnsHow to count unique records by two columns in pandas?
Why is unzipped file smaller than zipped file
Find this Unique UVC Palindrome ( ignoring signs and decimal) from Given Fractional Relationship
Is it normal to "extract a paper" from a master thesis?
How to test if argument is a single space?
Ratings matrix plot
Computing elements of a 1000 x 60 matrix exhausts RAM
If a character has cast the Fly spell on themselves, can they "hand off" to the Levitate spell without interruption?
What is this dime sized black bug with white on the segments near Loveland Colorodao?
Proto-Indo-European (PIE) words with IPA
VHDL: Why is it hard to design a floating point unit in hardware?
Real Analysis: Proof of the equivalent definitions of the derivative.
Does science define life as "beginning at conception"?
How to eliminate gap at the start and at the end of a line when it's drawn along a side of a node's bounding box?
why "American-born", not "America-born"?
What is the winged creature on the back of the Mordenkainen's Tome of Foes book?
Why "strap-on" boosters, and how do other people say it?
Why A=2 and B=1 in the call signs for Spirit and Opportunity?
Keeping the dodos out of the field
Illustrating that universal optimality is stronger than sphere packing
Why do testers need root cause analysis?
How to tease a romance without a cat and mouse chase?
Which values for voltage divider
Is ideal gas incompressible?
Variable does not Exist: CaseTrigger
Get a count of combinations and its reverse from two columns
How to get count of two-way combinations from two columns?How to return multiple values from a function?Speed comparison with Project Euler: C vs Python vs Erlang vs HaskellPandas: create two new columns in a dataframe with values calculated from a pre-existing columnDelete column from pandas DataFrame by column name“Large data” work flows using pandasSelect rows from a DataFrame based on values in a column in pandasPandas DataFrame Groupby two columns and get countsGet list from pandas DataFrame column headerspandas create new column based on values from other columnsHow to count unique records by two columns in pandas?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I'm trying to get a count of combinations from a pandas dataframe where it views reversed form of the combinations as the same. ie (A/B will be the same as B/A)
Similar to what this user is trying to do, but on python/pandas
How to get count of two-way combinations from two columns?
Thank you for helping!
I've explored crosstabs and grouping the data and it produces a count of the combinations, but it sees the reverse order as a unique combination.
Origin Destination
City 1 City 2
City 2 City 1
City 3 City 4
City 2 City 1
End result will look like
Route Count
City 1 - City 2 3
City 3 - City 4 1
note: order of the route does not matter. It could be City 2 - City 1, as long as it counts it as the same.
python pandas
add a comment |
I'm trying to get a count of combinations from a pandas dataframe where it views reversed form of the combinations as the same. ie (A/B will be the same as B/A)
Similar to what this user is trying to do, but on python/pandas
How to get count of two-way combinations from two columns?
Thank you for helping!
I've explored crosstabs and grouping the data and it produces a count of the combinations, but it sees the reverse order as a unique combination.
Origin Destination
City 1 City 2
City 2 City 1
City 3 City 4
City 2 City 1
End result will look like
Route Count
City 1 - City 2 3
City 3 - City 4 1
note: order of the route does not matter. It could be City 2 - City 1, as long as it counts it as the same.
python pandas
add a comment |
I'm trying to get a count of combinations from a pandas dataframe where it views reversed form of the combinations as the same. ie (A/B will be the same as B/A)
Similar to what this user is trying to do, but on python/pandas
How to get count of two-way combinations from two columns?
Thank you for helping!
I've explored crosstabs and grouping the data and it produces a count of the combinations, but it sees the reverse order as a unique combination.
Origin Destination
City 1 City 2
City 2 City 1
City 3 City 4
City 2 City 1
End result will look like
Route Count
City 1 - City 2 3
City 3 - City 4 1
note: order of the route does not matter. It could be City 2 - City 1, as long as it counts it as the same.
python pandas
I'm trying to get a count of combinations from a pandas dataframe where it views reversed form of the combinations as the same. ie (A/B will be the same as B/A)
Similar to what this user is trying to do, but on python/pandas
How to get count of two-way combinations from two columns?
Thank you for helping!
I've explored crosstabs and grouping the data and it produces a count of the combinations, but it sees the reverse order as a unique combination.
Origin Destination
City 1 City 2
City 2 City 1
City 3 City 4
City 2 City 1
End result will look like
Route Count
City 1 - City 2 3
City 3 - City 4 1
note: order of the route does not matter. It could be City 2 - City 1, as long as it counts it as the same.
python pandas
python pandas
asked Mar 23 at 21:10
Tony NguyenTony Nguyen
1
1
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You can define a route using np.sort
import numpy as np
import pandas as pd
df['Route'] = [' - '.join(x) for x in np.sort(df.to_numpy(), axis=1)]
df.groupby('Route').size()
#Route
#City 1 - City 2 3
#City 3 - City 4 1
#dtype: int64
You can also construct a new sorted DataFrame
, which could be useful:
df = pd.DataFrame(np.sort(df.to_numpy(), axis=1), index=df.index, columns=df.columns)
# Origin Destination
#0 City 1 City 2
#1 City 1 City 2
#2 City 3 City 4
#3 City 1 City 2
Now you can groupby ['Origin', 'Destintion']
Worked like a charm. Thank you Alollz. You are a GENIUS!!!!
– Tony Nguyen
Mar 23 at 22:39
add a comment |
Check with sort
df.values.sort()
df.groupby(list(df)).size()
Origin Destination
City1 City2 3
City3 City4 1
dtype: int64
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%2f55318424%2fget-a-count-of-combinations-and-its-reverse-from-two-columns%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
You can define a route using np.sort
import numpy as np
import pandas as pd
df['Route'] = [' - '.join(x) for x in np.sort(df.to_numpy(), axis=1)]
df.groupby('Route').size()
#Route
#City 1 - City 2 3
#City 3 - City 4 1
#dtype: int64
You can also construct a new sorted DataFrame
, which could be useful:
df = pd.DataFrame(np.sort(df.to_numpy(), axis=1), index=df.index, columns=df.columns)
# Origin Destination
#0 City 1 City 2
#1 City 1 City 2
#2 City 3 City 4
#3 City 1 City 2
Now you can groupby ['Origin', 'Destintion']
Worked like a charm. Thank you Alollz. You are a GENIUS!!!!
– Tony Nguyen
Mar 23 at 22:39
add a comment |
You can define a route using np.sort
import numpy as np
import pandas as pd
df['Route'] = [' - '.join(x) for x in np.sort(df.to_numpy(), axis=1)]
df.groupby('Route').size()
#Route
#City 1 - City 2 3
#City 3 - City 4 1
#dtype: int64
You can also construct a new sorted DataFrame
, which could be useful:
df = pd.DataFrame(np.sort(df.to_numpy(), axis=1), index=df.index, columns=df.columns)
# Origin Destination
#0 City 1 City 2
#1 City 1 City 2
#2 City 3 City 4
#3 City 1 City 2
Now you can groupby ['Origin', 'Destintion']
Worked like a charm. Thank you Alollz. You are a GENIUS!!!!
– Tony Nguyen
Mar 23 at 22:39
add a comment |
You can define a route using np.sort
import numpy as np
import pandas as pd
df['Route'] = [' - '.join(x) for x in np.sort(df.to_numpy(), axis=1)]
df.groupby('Route').size()
#Route
#City 1 - City 2 3
#City 3 - City 4 1
#dtype: int64
You can also construct a new sorted DataFrame
, which could be useful:
df = pd.DataFrame(np.sort(df.to_numpy(), axis=1), index=df.index, columns=df.columns)
# Origin Destination
#0 City 1 City 2
#1 City 1 City 2
#2 City 3 City 4
#3 City 1 City 2
Now you can groupby ['Origin', 'Destintion']
You can define a route using np.sort
import numpy as np
import pandas as pd
df['Route'] = [' - '.join(x) for x in np.sort(df.to_numpy(), axis=1)]
df.groupby('Route').size()
#Route
#City 1 - City 2 3
#City 3 - City 4 1
#dtype: int64
You can also construct a new sorted DataFrame
, which could be useful:
df = pd.DataFrame(np.sort(df.to_numpy(), axis=1), index=df.index, columns=df.columns)
# Origin Destination
#0 City 1 City 2
#1 City 1 City 2
#2 City 3 City 4
#3 City 1 City 2
Now you can groupby ['Origin', 'Destintion']
answered Mar 23 at 21:23
ALollzALollz
18.5k51840
18.5k51840
Worked like a charm. Thank you Alollz. You are a GENIUS!!!!
– Tony Nguyen
Mar 23 at 22:39
add a comment |
Worked like a charm. Thank you Alollz. You are a GENIUS!!!!
– Tony Nguyen
Mar 23 at 22:39
Worked like a charm. Thank you Alollz. You are a GENIUS!!!!
– Tony Nguyen
Mar 23 at 22:39
Worked like a charm. Thank you Alollz. You are a GENIUS!!!!
– Tony Nguyen
Mar 23 at 22:39
add a comment |
Check with sort
df.values.sort()
df.groupby(list(df)).size()
Origin Destination
City1 City2 3
City3 City4 1
dtype: int64
add a comment |
Check with sort
df.values.sort()
df.groupby(list(df)).size()
Origin Destination
City1 City2 3
City3 City4 1
dtype: int64
add a comment |
Check with sort
df.values.sort()
df.groupby(list(df)).size()
Origin Destination
City1 City2 3
City3 City4 1
dtype: int64
Check with sort
df.values.sort()
df.groupby(list(df)).size()
Origin Destination
City1 City2 3
City3 City4 1
dtype: int64
answered Mar 24 at 0:10
WeNYoBenWeNYoBen
135k84474
135k84474
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%2f55318424%2fget-a-count-of-combinations-and-its-reverse-from-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