具有组内值的新列(New column with a value inside a group)

       A           B    C
0    Red  2002-01-13  3.9
1    Red  2002-01-13  4.1
2    Red  2002-01-13  2.3
3    Red  2002-01-14  0.7
4    Red  2002-01-14  5.9
5    Red  2002-01-14  3.0
6    Red  2002-01-15  6.9
7    Red  2002-01-15  6.4
8    Red  2002-01-15  9.9
9   Blue  2006-07-21  7.2
10  Blue  2006-07-21  4.2
11  Blue  2006-07-21  6.1
12  Blue  2006-07-22  0.1
13  Blue  2006-07-22  3.2
14  Blue  2006-07-22  2.7

我想用下一个条件创建一个df['D']列:

  • 取每个A组的第一个B日期的最后一个C值。

我的意思是,对于ARed ,取2.3,因为它是第一个B date 2002-01-13的最后一个C值。 对于ABlue ,取6.1,因为它是2006-07-21第一个B日期的最后一个C值。

输出应该是:

       A           B    C    D
0    Red  2002-01-13  3.9  2.3
1    Red  2002-01-13  4.1  2.3
2    Red  2002-01-13  2.3  2.3
3    Red  2002-01-14  0.7  2.3
4    Red  2002-01-14  5.9  2.3
5    Red  2002-01-14  3.0  2.3
6    Red  2002-01-15  6.9  2.3
7    Red  2002-01-15  6.4  2.3
8    Red  2002-01-15  9.9  2.3
9   Blue  2006-07-21  7.2  6.1
10  Blue  2006-07-21  4.2  6.1
11  Blue  2006-07-21  6.1  6.1
12  Blue  2006-07-22  0.1  6.1
13  Blue  2006-07-22  3.2  6.1
14  Blue  2006-07-22  2.7  6.1

我试过了:

df['D'] = df.groupby('A')["C"].transform('last')

       A           B    C
0    Red  2002-01-13  3.9
1    Red  2002-01-13  4.1
2    Red  2002-01-13  2.3
3    Red  2002-01-14  0.7
4    Red  2002-01-14  5.9
5    Red  2002-01-14  3.0
6    Red  2002-01-15  6.9
7    Red  2002-01-15  6.4
8    Red  2002-01-15  9.9
9   Blue  2006-07-21  7.2
10  Blue  2006-07-21  4.2
11  Blue  2006-07-21  6.1
12  Blue  2006-07-22  0.1
13  Blue  2006-07-22  3.2
14  Blue  2006-07-22  2.7

I want to create a df['D'] column, with next condition:

  • Take the last C value of the first B date, by each A group.

I mean, for A group Red, take 2.3, as it is the last C value of first B date 2002-01-13. For A group Blue, take 6.1, as it is the last C value of first B date 2006-07-21.

Output should be:

       A           B    C    D
0    Red  2002-01-13  3.9  2.3
1    Red  2002-01-13  4.1  2.3
2    Red  2002-01-13  2.3  2.3
3    Red  2002-01-14  0.7  2.3
4    Red  2002-01-14  5.9  2.3
5    Red  2002-01-14  3.0  2.3
6    Red  2002-01-15  6.9  2.3
7    Red  2002-01-15  6.4  2.3
8    Red  2002-01-15  9.9  2.3
9   Blue  2006-07-21  7.2  6.1
10  Blue  2006-07-21  4.2  6.1
11  Blue  2006-07-21  6.1  6.1
12  Blue  2006-07-22  0.1  6.1
13  Blue  2006-07-22  3.2  6.1
14  Blue  2006-07-22  2.7  6.1

I've tried:

df['D'] = df.groupby('A')["C"].transform('last')

原文:https://stackoverflow.com/questions/51450485
2024-04-22 21:04

相关问答

更多

如何从Gmail下载许多电子邮件附件(How to download many email attachments from gmail)

您可以使用Gmail API检索附件: https : //developers.google.com/gmail/api/v1/reference/users/messages/attachments/get#examples You can use Gmail API to retrieve attachments: https://developers.google.com/gmail/api/v1/reference/users/messages/attachments/get#examp...

如何仅从特定Gmail邮件标签下载未读附件?(How do I download only unread attachments from a specific gmail label?)

尝试修改此行: resp, items = m.search(None, 'FROM', '"Impact Stats Script"') 至: resp, items = m.search(None, 'UNSEEN', 'FROM', '"Impact Stats Script"') Python imaplib文档仅显示添加更多搜索条件 , IMAP规范定义了UNSEEN搜索条件: UNSEEN Messages that do not have the \Seen fla...

如何从Gmail附件下载所有电子邮件?(How can I download all emails with attachments from Gmail?)

硬一个:-) import email, getpass, imaplib, os detach_dir = '.' # directory where to save attachments (default: current) user = raw_input("Enter your GMail username:") pwd = getpass.getpass("Enter your password: ") # connecting to the gmail imap server m...

如何从Gmail下载电子邮件(How to download an email from gmail)

有关rebol / command的spop协议,请参见http://re-bol.com/prot-spop.r 。 我也有一个Rebol3的版本。 来自Saphir / Atronix的R3有https。 See http://re-bol.com/prot-spop.r for the spop protocol for rebol/command. I also have a version for Rebol3 somewhere. R3 from Saphir/Atronix has ...

如何在不删除电子邮件的情况下删除Gmail附件?(How Remove Gmail Attachments In Place Without Deleting Email?)

官方文档解释说您只能修改消息的标签 。 您将无法更新邮件正文。 无论如何,你需要做的是获取消息数据并保存它,然后删除消息 ,最后再次插入消息 ,删除附件。 我希望这个信息帮助! The official documentation explains that you can only modify the labels of the message. You won't be able to update the message body. In any case, what you are re...

如何获取电子邮件附件元数据而不是Gmail中的内容?(How to get email attachment meta data but not the content from Gmail?)

最后,我放弃了AE.Net.Mail并转而使用Gmail API。 我可以从消息获取请求中获取附件元数据,而无需获取实际的附件文件。 https://developers.google.com/gmail/api/v1/reference/users/messages/get Eventually, I gave up on AE.Net.Mail and switched to Gmail API instead. And I am able to get attachment meta dat...

使用php mail()函数发送电子邮件附件 - 为gmail工作,但其他邮件提供商的附件大小为零(0字节)(Email Attachments using php mail() function - Working for gmail but attachments are of null size (0 bytes) for other mailing provider)

我更喜欢将HTML用于此类目的。 您可以使用电子邮件中的锚标记为服务器上的文件指定链接。 要在电子邮件中编写HTML: <?php $to = "somebody@example.com, somebodyelse@example.com"; $subject = "HTML email"; $message = " <html> <head> <title>HTML email</title> </head> <body> <p>This email contains HTML Tags!</...

脚本下载电子邮件附件(script to download email attachments)

email模块包含一个函数message_from_bytes 。 使用它来代替message_from_string来解析bytes对象。 m = email.message_from_bytes(email_body) The email module includes a function message_from_bytes. Use that instead of message_from_string to parse a bytes object. m = email.messag...

通过gmail通过电子邮件发送R中的附件(使用jython)(Emailing attachments in R via gmail (using jython))

ENOENT表示找不到文件 - 您的代码只接受files的第一个字符,当然 - 它不是有效的文件名。 要使用jython通过带有PDF附件的Gmail成功发送电子邮件,您可以使用: library(rJython) rJython <- rJython() rJython$exec( "import smtplib" ) rJython$exec("from email.MIMEMultipart import MIMEMultipart") rJython$exec("from email.M...

从Outlook 2007下载电子邮件附件(Download Email Attachments from Outlook 2007)

那么您在Exchange 2007/2010环境中使用Outlook? 如果是,你冷看看EWS 。 So you are using outlook in an Exchange 2007/2010 environment? If yes you cold take a look at EWS.

相关文章

更多

inside MPQ

翻译前声明: 本翻译对于原文进行了适量删节和修改。 本翻译只做为学习参考使用,不得用于任何商业目的 ...

Becoming a data scientist

Data Week: Becoming a data scientist Data Pointed, ...

[转]So You Want To Be A Producer

pro-du-cer n. 1. Someone from a game publisher who ...

SQL中GROUP BY与HAVING的用法

显示每个地区的总人口数和总面积. SELECT region, SUM(population), SU ...

求一个group by后面字段顺序影响结果的例子

select a,b ,sum(e) from test group by a,b order by ...

Spring Data: a new perspective of data operations

Spring Data: a new perspective of data operations ...

How to Start a Business in 10 Days

With an executive staffing venture about to open, a ...

A Great List of Windows Tools

Windowsis an extremely effective and a an efficient ...

Create a Bootable MicroSD Card

http://gumstix.org/create-a-bootable-microsd-card.h ...

[译文] 恶意软件行为综述 - A View on Current Malware Behaviors

A View on Current Malware Behaviors Ulrich Bayer ...

最新问答

更多

python的访问器方法有哪些

使用方法: class A(object): def foo(self,x): #类实例方法 print "executing foo(%s,%s)"%(self,x) @classmethod def class_foo(cls,x): #类方法 print "executing class_foo(%s,%s)"%(cls,x) @staticmethod def static_foo(x): #静态方法 print "executing static_foo(%s)"%x调用方法: a =

使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)

我认为你必须将两个TableGetway传递给UserTable构造。 你必须改变Module.php看看: public function getServiceConfig() { return array( 'factories' => array( 'User\Model\UserTable' => function($sm) { $userTableGateway = $sm->get('UserTable

透明度错误IE11(Transparency bug IE11)

这是一个渲染错误,由使用透明度触发,使用bootstrap用于在聚焦控件周围放置蓝色光环的box-shadow属性。 可以通过添加以下类覆盖来解决它。 .form-control:hover { -webkit-box-shadow: 0px 0px 5px 0px rgba(0,0,255,1); -moz-box-shadow: 0px 0px 5px 0px rgba(0,0,255,1); box-shadow: 0px 0px 4px 0px rgba(0,0,255,1)

linux的基本操作命令。。。

ls 显示目录 mkdir 建立目录 cd 进入目录

响应navi重叠h1和nav上的h1链接不起作用(Responsive navi overlaps h1 and navi links on h1 isn't working)

将z-index设置为.main-nav这将解决您的重叠问题 .main-nav { position:relative; z-index:9; } set z-index to .main-nav This will fix your overlaping issue .main-nav { position:relative; z-index:9; }

在C中读取文件:“r”和“a +”标志的不同行为(Reading a File in C: different behavior for “r” and “a+” flags)

这是因为模式规范"a"打开一个文件以便追加,文件指针在末尾。 如果您尝试从此处读取,则由于文件指针位于EOF,因此没有数据。 您应该打开"r+"进行阅读和写作。 如果在写入之前读取整个文件,则在写入更多数据时,文件指针将正确定位以追加。 如果这还不够,请探索ftell()和fseek()函数。 That is because the mode spec "a" opens a file for appending, with the file pointer at the end. If you

NFC提供什么样的带宽?(What Kind of Bandwidth does NFC Provide?)

支持空中接口的数据速率是一回事。 在消除协议开销,等待eeprom写入以及所有需要时间的其他内容之后,您看到的数据速率是完全不同的故事。 长话短说,从标签读取或进行对等传输时的实际数据速率峰值约为2.5千字节/秒。 取决于具体的标签或对等技术,它可能比这慢很多。 The supported data-rates of the air-interface are one thing. The data-rate that you see after removing protocol overhe

元素上的盒子阴影行为(box-shadow behaviour on elements)

它看起来像只在Windows上的Chrome的错误。 我在Google Canary (Chrome 63)中也进行了测试,问题依然存在,所以有可能它不会很快修复。 这个问题是由overflow: auto引起的overflow: auto ,在你的情况下,它可以很容易地通过删除或设置为可见(默认)来解决。 但是 ,将鼠标悬停在右侧(顶部和底部)时,会出现滚动条。 一个解决方案可以设置overflow: hidden的身体,所以预期的结果是所需的。 我想指出,这不是一个很好的解决方案,但我建议暂

Laravel检查是否存在记录(Laravel Checking If a Record Exists)

这取决于您是否要以后与用户合作,或仅检查是否存在。 如果要使用用户对象(如果存在): $user = User::where('email', '=', Input::get('email'))->first(); if ($user === null) { // user doesn't exist } 如果你只想检查 if (User::where('email', '=', Input::get('email'))->count() > 0) { // user found

设置base64图像的大小javascript - angularjs(set size of a base64 image javascript - angularjs)

$scope.getData= function () { var reader = new FileReader(); reader.onload = $('input[type=file]')[0].files; var img = new Image(); img.src =(reader.onload[0].result); img.onload = function() { if(this.width > 640