Assign Name to Derived Column in Python Unicorn Meta Zoo #1: Why another podcast? Announcing the arrival of Valued Associate #679: Cesar Manara Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Calling an external command in PythonWhat are metaclasses in Python?Finding the index of an item given a list containing it in PythonWhat is the difference between Python's list methods append and extend?How can I safely create a nested directory in Python?Does Python have a ternary conditional operator?Does Python have a string 'contains' substring method?Renaming columns in pandasDelete column from pandas DataFrame by column nameSelect rows from a DataFrame based on values in a column in pandas
Is it accepted to use working hours to read general interest books?
Why I cannot instantiate a class whose constructor is private in a friend class?
How was Lagrange appointed professor of mathematics so early?
Determinant of a matrix with 2 equal rows
How would it unbalance gameplay to rule that Weapon Master allows for picking a fighting style?
Married in secret, can marital status in passport be changed at a later date?
Can gravitational waves pass through a black hole?
Where did Arya get these scars?
What's the difference between using dependency injection with a container and using a service locator?
TV series episode where humans nuke aliens before decrypting their message that states they come in peace
Are these square matrices always diagonalisable?
What helicopter has the most rotor blades?
Could a cockatrice have parasitic embryos?
Coin Game with infinite paradox
Does a Draconic Bloodline sorcerer's doubled proficiency bonus for Charisma checks against dragons apply to all dragon types or only the chosen one?
When speaking, how do you change your mind mid-sentence?
Getting AggregateResult variables from Execute Anonymous Window
using NDEigensystem to solve the Mathieu equation
Array Dynamic resize in heap
What *exactly* is electrical current, voltage, and resistance?
What is a 'Key' in computer science?
Is a self contained air-bullet cartridge feasible?
How to keep bees out of canned beverages?
How do I deal with an erroneously large refund?
Assign Name to Derived Column in Python
Unicorn Meta Zoo #1: Why another podcast?
Announcing the arrival of Valued Associate #679: Cesar Manara
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!Calling an external command in PythonWhat are metaclasses in Python?Finding the index of an item given a list containing it in PythonWhat is the difference between Python's list methods append and extend?How can I safely create a nested directory in Python?Does Python have a ternary conditional operator?Does Python have a string 'contains' substring method?Renaming columns in pandasDelete column from pandas DataFrame by column nameSelect rows from a DataFrame based on values in a column in pandas
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I am new to Python. I have read two columns say nTime
and nMessageId
from a table using read_sql
function and stored it in variable. for e.g.
df = pd.read_sql(query)
Result for this is:
nTime nMessageId
22/03/2019 08:31:00 2552
22/03/2019 08:31:00 1025
22/03/2019 08:31:00 2552
22/03/2019 08:31:00 1342
22/03/2019 08:31:01 1342
22/03/2019 08:31:02 128
Then further I have derived top 10 records based on the count of the records on column nTime
using value_counts(ascending = False).head(10)
function. i.e.
df2 = df['nTime'].value_counts(ascending = False).head(10)
Then the output may be
22/03/2019 08:31:00 882
22/03/2019 08:31:01 342
22/03/2019 08:31:02 128
Now I want to do inner join of df['nTime']
and df2
and derive count of each nMessage
i.e. at 22/03/2019 08:31:00
for nMessageId= 2552
the count of records will be 2.
How do I achieve the above using merge function or any other way as I am not getting column name in df2
?
Please help.
python pandas dataframe merge
add a comment |
I am new to Python. I have read two columns say nTime
and nMessageId
from a table using read_sql
function and stored it in variable. for e.g.
df = pd.read_sql(query)
Result for this is:
nTime nMessageId
22/03/2019 08:31:00 2552
22/03/2019 08:31:00 1025
22/03/2019 08:31:00 2552
22/03/2019 08:31:00 1342
22/03/2019 08:31:01 1342
22/03/2019 08:31:02 128
Then further I have derived top 10 records based on the count of the records on column nTime
using value_counts(ascending = False).head(10)
function. i.e.
df2 = df['nTime'].value_counts(ascending = False).head(10)
Then the output may be
22/03/2019 08:31:00 882
22/03/2019 08:31:01 342
22/03/2019 08:31:02 128
Now I want to do inner join of df['nTime']
and df2
and derive count of each nMessage
i.e. at 22/03/2019 08:31:00
for nMessageId= 2552
the count of records will be 2.
How do I achieve the above using merge function or any other way as I am not getting column name in df2
?
Please help.
python pandas dataframe merge
df["count"] = df.groupby(["nMessageId", "nTime"]).transform("size")
– PMende
Mar 22 at 17:28
add a comment |
I am new to Python. I have read two columns say nTime
and nMessageId
from a table using read_sql
function and stored it in variable. for e.g.
df = pd.read_sql(query)
Result for this is:
nTime nMessageId
22/03/2019 08:31:00 2552
22/03/2019 08:31:00 1025
22/03/2019 08:31:00 2552
22/03/2019 08:31:00 1342
22/03/2019 08:31:01 1342
22/03/2019 08:31:02 128
Then further I have derived top 10 records based on the count of the records on column nTime
using value_counts(ascending = False).head(10)
function. i.e.
df2 = df['nTime'].value_counts(ascending = False).head(10)
Then the output may be
22/03/2019 08:31:00 882
22/03/2019 08:31:01 342
22/03/2019 08:31:02 128
Now I want to do inner join of df['nTime']
and df2
and derive count of each nMessage
i.e. at 22/03/2019 08:31:00
for nMessageId= 2552
the count of records will be 2.
How do I achieve the above using merge function or any other way as I am not getting column name in df2
?
Please help.
python pandas dataframe merge
I am new to Python. I have read two columns say nTime
and nMessageId
from a table using read_sql
function and stored it in variable. for e.g.
df = pd.read_sql(query)
Result for this is:
nTime nMessageId
22/03/2019 08:31:00 2552
22/03/2019 08:31:00 1025
22/03/2019 08:31:00 2552
22/03/2019 08:31:00 1342
22/03/2019 08:31:01 1342
22/03/2019 08:31:02 128
Then further I have derived top 10 records based on the count of the records on column nTime
using value_counts(ascending = False).head(10)
function. i.e.
df2 = df['nTime'].value_counts(ascending = False).head(10)
Then the output may be
22/03/2019 08:31:00 882
22/03/2019 08:31:01 342
22/03/2019 08:31:02 128
Now I want to do inner join of df['nTime']
and df2
and derive count of each nMessage
i.e. at 22/03/2019 08:31:00
for nMessageId= 2552
the count of records will be 2.
How do I achieve the above using merge function or any other way as I am not getting column name in df2
?
Please help.
python pandas dataframe merge
python pandas dataframe merge
edited Mar 22 at 15:42
TrebledJ
4,04821432
4,04821432
asked Mar 22 at 15:06
SaumikSaumik
61
61
df["count"] = df.groupby(["nMessageId", "nTime"]).transform("size")
– PMende
Mar 22 at 17:28
add a comment |
df["count"] = df.groupby(["nMessageId", "nTime"]).transform("size")
– PMende
Mar 22 at 17:28
df["count"] = df.groupby(["nMessageId", "nTime"]).transform("size")
– PMende
Mar 22 at 17:28
df["count"] = df.groupby(["nMessageId", "nTime"]).transform("size")
– PMende
Mar 22 at 17:28
add a comment |
0
active
oldest
votes
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%2f55302575%2fassign-name-to-derived-column-in-python%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f55302575%2fassign-name-to-derived-column-in-python%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
df["count"] = df.groupby(["nMessageId", "nTime"]).transform("size")
– PMende
Mar 22 at 17:28