- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个看起来像这样的 DataFrame(但有 149110 行):
df = {'group':['a','a','a','a',
'b','b','b','b','b','b','b','b','b',
'c','c','c','c','c',
'd','d','d','d','d','d','d',
'e','e','e','e',],
'date':[np.datetime64('2020-01-01'),np.datetime64('2020-01-01'),np.datetime64('2020-01-01'),np.datetime64('2020-01-01'),
np.datetime64('2019-03-12'),np.datetime64('2019-03-12'),np.datetime64('2019-03-12'),np.datetime64('2019-03-12'),
np.datetime64('2019-03-12'),np.datetime64('2019-03-12'),np.datetime64('2019-03-12'),np.datetime64('2019-03-12'),
np.datetime64('2019-03-12'),
np.datetime64('2020-01-01'),np.datetime64('2020-01-01'),np.datetime64('2020-01-01'),np.datetime64('2020-01-01'),
np.datetime64('2020-01-01'),
np.datetime64('2019-01-17'),np.datetime64('2019-01-17'),np.datetime64('2019-01-17'),np.datetime64('2019-01-17'),
np.datetime64('2019-01-17'),np.datetime64('2019-01-17'),np.datetime64('2019-01-17'),
np.datetime64('2018-12-03'),np.datetime64('2018-12-03'),np.datetime64('2018-12-03'),np.datetime64('2018-12-03')],
'id':['tom','taliha','alyssa','randyl',
'tom','taliha','edward','aaron','daniel','jean','sigmund','albus','riddle',
'fellicia','ron','fred','george','alex',
'taliha','alyssa','locke','jon','jamie','sam','sydney',
'jon','jamie','sam','arya'],
'value':[1,2,3,4,
7,6,4,8,2,3,5,9,1,
1,2,3,4,5,
5,7,6,3,4,1,2,
3,2,1,4]}
df= pd.DataFrame(df)
df
group date id value
0 a 2020-01-01 tom 1
1 a 2020-01-01 taliha 2
2 a 2020-01-01 alyssa 3
3 a 2020-01-01 randyl 4
4 b 2019-03-12 tom 7
5 b 2019-03-12 taliha 6
6 b 2019-03-12 edward 4
7 b 2019-03-12 aaron 8
8 b 2019-03-12 daniel 2
9 b 2019-03-12 jean 3
10 b 2019-03-12 sigmund 5
11 b 2019-03-12 albus 9
12 b 2019-03-12 riddle 1
13 c 2020-01-01 fellicia1
14 c 2020-01-01 ron 2
15 c 2020-01-01 fred 3
16 c 2020-01-01 george 4
17 c 2020-01-01 alex 5
18 d 2019-01-17 taliha 5
19 d 2019-01-17 alyssa 7
20 d 2019-01-17 locke 6
21 d 2019-01-17 jon 3
22 d 2019-01-17 jamie 4
23 d 2019-01-17 sam 1
24 d 2019-01-17 sydney 2
25 e 2018-12-03 jon 3
26 e 2018-12-03 jamie 2
27 e 2018-12-03 sam 1
28 e 2018-12-03 arya 4
我需要一个列:together
,如果此人与当前组中的另一个人在一个组中,但在过去一年中,则返回 1。
例如,在“a”组中,我们有 4 个人,但 tom 和 taliha 都在 np.datetime64('2016-03-12')
中,即他们都在将'b'组在一起。我们还可以看到,在“c”组中,taliha 和 alyssa 也在一起。所以我希望组 a 的 together
的相应值在 tom、taliha 和 alyssa 旁边有一个 1,但是 randyl 的值是 0,因为他过去没有和其他任何人一起组过年。
然后对于 'b' 和 'c' 组,因为在过去的一年中没有人与其他任何人在一个组中,我希望 together
的值对每个人都为 0。
对于去年的“d”组,我们可以看到 jon、jamie 和 sam 在同一组中,即他们是“e”组的一部分。因此,“d”组中 jon、jamie 和 sam 的 together
值应该为 1,其余人的值为 0。
因为在'e'组之前没有数据,所以它们应该都被赋值为0。
然后我想创建另一个新列:rel
,这取决于人们在前一组中的值(value)。如果过去组中的那个人的值低于另一个人,我希望 rel
等于 1,如果他们的值更高,则为 -1。
例如,在组 'a' 中,tom 的 rel
值应该是 -1,因为他的 value
比 'b' 中的 taliha 高,因此taliha 的 rel
值应该为 1,因为她的 value
低于 'b' 中的 tom。对于 alyssa,我希望将 rel
的值设置为 -1,因为在组 d 中,她的 value
高于 talhia。
基本上,值
越低越好。我正在尝试根据过去的值(value)
对人们进行排名。所以对于“a”组,我需要一个基本上可以显示的系统塔丽哈 > 汤姆和塔丽哈 > 艾丽莎。但是我们不知道 tom 和 alyssa 之间的关系,所以我将它们视为相同的值。我也不知道 randyl 和组“a”中的其他人之间的关系,所以我想为他将 rel
的值设置为 0。例如,如果我发现这样的关系:人 1 > 人 2 > 人 3 并且没有人 4 的历史记录。我想要 rel
来反射(reflect)他的关系。我希望 rel
的值看起来有点像这个人 1 = 2、人 2 = 0、人 3 = -2 和人 4 = 0。
所以我希望生成的 DataFrame 看起来像这样:
group date id value together rel
0 a 2020-01-01 tom 1 1 -1
1 a 2020-01-01 taliha 2 1 1
2 a 2020-01-01 alyssa 3 1 -1
3 a 2020-01-01 randyl 4 0 0
4 b 2019-03-12 tom 7 0 0
5 b 2019-03-12 taliha 6 0 0
6 b 2019-03-12 edward 4 0 0
7 b 2019-03-12 aaron 8 0 0
8 b 2019-03-12 daniel 2 0 0
9 b 2019-03-12 jean 3 0 0
10 b 2019-03-12 sigmund 5 0 0
11 b 2019-03-12 albus 9 0 0
12 b 2019-03-12 riddle 1 0 0
13 c 2020-01-01 fellicia1 0 0
14 c 2020-01-01 ron 2 0 0
15 c 2020-01-01 fred 3 0 0
16 c 2020-01-01 george 4 0 0
17 c 2020-01-01 alex 5 0 0
18 d 2019-01-17 taliha 5 0 0
19 d 2019-01-17 alyssa 7 0 0
20 d 2019-01-17 locke 6 0 0
21 d 2019-01-17 jon 3 1 -2
22 d 2019-01-17 jamie 4 1 0
23 d 2019-01-17 sam 1 1 2
24 d 2019-01-17 sydney 2 0 0
25 e 2018-12-03 jon 3 0 0
26 e 2018-12-03 jamie 2 0 0
27 e 2018-12-03 sam 1 0 0
28 e 2018-12-03 arya 4 0 0
最佳答案
我试试看。第一个任务似乎很容易,第二个任务让我头疼。我对第二部分的结果与您的预期略有不同。也许你犯了错误,但很可能是我的误解。
from itertools import combinations
df_grps = df.groupby([df.date.dt.year, 'group']).id.apply(set)
df_vals = df.set_index([df.date.dt.year, 'group', 'id']).value
results = {}
for year in sorted(df.date.dt.year.unique())[1:]:
groups = {}
for group in df_grps.loc[year].index:
ids = df_grps.loc[year, group]
together = set().union(*(
i for i in (ids & h for h in df_grps.loc[year-1]) if len(i) > 1
))
if not together:
continue
together = {i: 0 for i in together}
for i, j in combinations(together, 2):
for group_old in df_grps.loc[year-1].index:
if not {i, j} <= df_grps.at[year-1, group_old]:
continue
i_val = df_vals.at[year-1, group_old, i]
j_val = df_vals.at[year-1, group_old, j]
if i_val < j_val:
together[i] += 1
together[j] -= 1
elif i_val > j_val:
together[i] -= 1
together[j] += 1
groups[group] = together
if groups:
results[year] = groups
df_res = pd.DataFrame(
[
[year, group, i, r]
for year, groups in results.items()
for group, rel in groups.items()
for i, r in rel.items()
],
columns=['date', 'group', 'id', 'rel']
).set_index(['date', 'group', 'id'])
df.set_index([df.date.dt.year, 'group', 'id'], inplace=True)
df['together'], df['rel'] = 0, 0
df.loc[df_res.index, 'together'] = 1
df.loc[df_res.index, 'rel'] = df_res.rel
样本框架的结果:
date value together rel
date group id
2020 a tom 2020-01-01 1 1 -1
taliha 2020-01-01 2 1 2
alyssa 2020-01-01 3 1 -1
randyl 2020-01-01 4 0 0
2019 b tom 2019-03-12 7 0 0
taliha 2019-03-12 6 0 0
edward 2019-03-12 4 0 0
aaron 2019-03-12 8 0 0
daniel 2019-03-12 2 0 0
jean 2019-03-12 3 0 0
sigmund 2019-03-12 5 0 0
albus 2019-03-12 9 0 0
riddle 2019-03-12 1 0 0
2020 c fellicia 2020-01-01 1 0 0
ron 2020-01-01 2 0 0
fred 2020-01-01 3 0 0
george 2020-01-01 4 0 0
alex 2020-01-01 5 0 0
2019 d taliha 2019-01-17 5 0 0
alyssa 2019-01-17 7 0 0
locke 2019-01-17 6 0 0
jon 2019-01-17 3 1 -2
jamie 2019-01-17 4 1 0
sam 2019-01-17 1 1 2
sydney 2019-01-17 2 0 0
2018 e jon 2018-12-03 3 0 0
jamie 2018-12-03 2 0 0
sam 2018-12-03 1 0 0
arya 2018-12-03 4 0 0
PS:我还有一个版本更多地保留在 Pandas 框架内,但它更长。如果您有兴趣,我会发布它。
关于python - 通过比较相同的行是否出现在数据框中的不同组中并分配相对值来在数据框中创建新列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68058701/
我的应用程序从一个有 5 个选项卡的选项卡栏 Controller 开始。一开始,第一个出现了它的名字,但其他四个没有名字,直到我点击它们。然后根据用户使用的语言显示名称。如何在选项卡栏出现之前设置选
我有嵌套数组 json 对象(第 1 层、第 2 层和第 3 层)。我的问题是数据表没有出现。任何相关的 CDN 均已导入。该表仅显示部分。我引用了很多网站,但都没有解决我的问题。 之前我使用标准表来
我正在尝试设置要显示的 Parse PFLoginViewController。这是我的一个 View Controller 的类。 import UIKit import Parse import
我遇到了这个问题,我绘制的对象没有出现在 GUI 中。我知道它正在被处理,因为数据被推送到日志文件。但是,图形没有出现。 这是我的一些代码: public static void main(Strin
我有一个树状图,其中包含出现这样的词...... TreeMap occurrence = new TreeMap (); 字符串 = 单词 整数 = 出现次数。 我如何获得最大出现次数 - 整数,
因此,我提示用户输入变量。如果变量小于 0 且大于 10。如果用户输入 10,我想要求用户再次输入数字。我问时间的时候输入4,它说你输入错误。但在第二次尝试时效果很好。例如:如果我输入 25,它会打印
我已经用 css overflow 属性做了一个例子。在这个例子中我遇到了一个溢出滚动的问题。滚动条出现了,但没有工作意味着每当将光标移动到滚动条时,在这个滚动条不活动的时间。我对此一无所知,所以请帮
我现在正在做一个元素。当您单击一个元素时,会出现以下信息,我想知道如何在您单击下一个元素而不重新单击同一元素时使其消失....例如,我的元素中有披萨,我想单击肉披萨看到浇头然后点击奶酪披萨看到浇头和肉
我有一个路由器模块,它将主题与正则表达式进行比较,并将出现的事件与一致的键掩码链接起来。 (它是一个简单的 url 路由过滤,如 symfony http://symfony.com/doc/curr
这个问题在这里已经有了答案: 9年前关闭。 Possible Duplicate: mysql_fetch_array() expects parameter 1 to be resource, bo
我在底部有一个带有工具栏的 View ,我正在使用 NavigationLink 导航到该 View 。但是当 View 出现时,工具栏显示得有点太低了。大约半秒钟后,它突然跳到位。它只会在应用程序启
我试图在我的应用程序上为背景音乐添加一个 AVAudioPlayer,我正在主屏幕上启动播放器,尝试在应用程序打开时开始播放但出现意外行为... 它播放并立即不断创建新玩家并播放这些玩家,因此同时播放
这是获取一个数字,获取其阶乘并将其加倍,但是由于基本情况,如果您输入 0,它会给出 2 作为答案,因此为了绕过它,我使用了 if 语句,但收到错误输入“if”时解析错误。如果你们能提供帮助,我真的很感
暂停期间抛出异常 android.os.DeadObjectException 在 android.os.BinderProxy.transactNative( native 方法) 在 androi
我已经为猜词游戏编写了一些代码。它从用户输入中读取字符并在单词中搜索该字符;根据字符是否在单词中,程序返回并控制一些变量。 代码如下: import java.util.Random; import
我是自动化领域的新手。这是我的简单 TestNG 登录代码,当我以 TestNG 身份运行该代码时,它会出现 java.lang.NullPointerException,双击它会突出显示我导航到 U
我是c#程序员,我习惯了c#的封装语法和其他东西。但是现在,由于某些原因,我应该用java写一些东西,我现在正在练习java一天!我要创建一个为我自己创建一个虚拟项目,以便让自己更熟悉 Java 的
我正在使用 Intellij,我的源类是 main.com.coding,我的资源文件是 main.com.testing。我将 spring.xml 文件放入资源文件中。 我的测试类位于 test.
我想要我的tests folder separate到我的应用程序代码。我的项目结构是这样的 myproject/ myproject/ myproject.py moduleon
这个问题已经有答案了: What is a NullPointerException, and how do I fix it? (12 个回答) 已关闭 6 年前。 因此,我尝试比较 2 个值,一个
我是一名优秀的程序员,十分优秀!