首页 \ 问答 \ 具有组内值的新列(New column with a value inside a group)

具有组内值的新列(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

相关问答

更多

相关文章

更多

最新问答

更多
  • 带有简单redis应用程序的Node.js抛出“未处理的错误”(Node.js with simple redis application throwing 'unhandled error')
  • 高考完可以去做些什么?注意什么?
  • Allauth不会保存其他字段(Allauth will not save additional fields)
  • Flask中的自定义中止映射/异常(Custom abort mapping/exceptions in Flask)
  • sed没有按预期工作,从字符串中间删除特殊字符(sed not working as expected, removing special character from middle of string)
  • 怎么在《我的世界》游戏里面编程
  • .NET可移植可执行文件VS .NET程序集(.NET Portable Executable File VS .NET Assembly)
  • 搜索字符串从视图中键入两个字段的“名字”和“姓氏”组合(Search Strings Typed from View for Two Fields 'First Name' and 'Last Name' Combined)
  • 我可以通过配置切换.Net缓存提供程序(Can I switch out .Net cache provider through configuration)
  • 在鼠标悬停或调整浏览器大小之前,内容不会加载(Content Does Not Load Until Mouse Hover or Resizing Browser)
  • 未捕获的TypeError:auth.get不是函数(Uncaught TypeError: auth.get is not a function)
  • 如何使用变量值创建参数类(How to create a parameter class with variant value)
  • 在std :: deque上并行化std :: replace(Parallelizing std::replace on std::deque)
  • 单元测试返回Connection对象的方法(Unit Test for a method that returns a Connection object)
  • rails:上传图片时ios中的服务器内部错误(rails: server internal error in ios while uploading image)
  • 如何在Android中构建应用程序警报[关闭](How build an application Alarm in Android [closed])
  • 以编程方式连接到Windows Mobile上的蓝牙耳机(Programmatically connect to bluetooth headsets on Windows Mobile)
  • 在两个不同的SharedPreference中编写并获得相同的结果(Writing in two different SharedPreference and getting the same result)
  • CSS修复容器和溢出元素(CSS Fix container and overflow elements)
  • 在'x','y','z'迭代上追加数组(Append array on 'x', 'y', 'z' iteration)
  • 我在哪里可以看到使用c ++源代码的UML方案示例[关闭](Where I can see examples of UML schemes with c++ source [closed])
  • SQL多个连接在与where子句相同的表上(SQL Multiple Joins on same table with where clause)
  • 位字段并集的大小,其成员数多于其大小(Size of bit-field union which has more members than its size)
  • 我安装了熊猫,但它不起作用(I installed pandas but it is not working)
  • Composer - 更改它在env中使用的PHP版本(Composer - Changing the version of PHP it uses in the env)
  • 使用JavaFX和Event获取鼠标位置(Getting a mouse position with JavaFX and Event)
  • 函数调用可以重新排序(Can function calls be reordered)
  • 关于“一对多”关系的NoSQL数据建模(NoSQL Data Modeling about “one to many” relationships)
  • 如何解释SBT错误消息(How to interpret SBT error messages)
  • 调试模式下的Sqlite编译器错误“初始化程序不是常量”(Sqlite compiler errors in Debug mode “initializer is not a constant”)