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;








0















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.










share|improve this question
























  • df["count"] = df.groupby(["nMessageId", "nTime"]).transform("size")

    – PMende
    Mar 22 at 17:28

















0















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.










share|improve this question
























  • df["count"] = df.groupby(["nMessageId", "nTime"]).transform("size")

    – PMende
    Mar 22 at 17:28













0












0








0








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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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

















  • 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












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
);



);













draft saved

draft discarded


















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















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현