- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对在 pandas 中使用 Wide_to_long 时的参数有疑问。有一个名为 suffix
的参数我不明白。
在文档中说:
suffix : str, default ‘\d+’
A regular expression capturing the wanted suffixes. ‘\d+’ captures numeric suffixes. Suffixes with no numbers could be specified with the negated character class ‘\D+’. You can also further disambiguate suffixes, for example, if your wide variables are of the form Aone, Btwo,.., and you have an unrelated column Arating, you can ignore the last one by specifying suffix=’(!?one|two)’
New in version 0.20.0.
问题:后缀可以用什么?
我发现有人在使用wide_to_long时使用这样的后缀:suffix='.'
它有什么作用?
最佳答案
TLDR:正则表达式捕获组可用于后缀参数。
suffix
参数告诉 pandas.wide_to_long
它应该根据 stub 后面的后缀在转换中包含哪些列。
从宽到长的默认行为假设您的列用数字标记,因此例如列A1、A2、A3、A4
无需指定后缀即可正常工作参数,而 Aone、Atwo、A Three、Afour
将失败。
正如所解释的,它还有各种其他用途,在极少数情况下,您的列可能是 A1、A2、A3、A4、A100
,并且您不想实际包含 A100
因为它实际上与其他 A#
列无关。
以下是一些说明性示例。
import pandas as pd
df = pd.DataFrame({'id': [1,2], 'A_1': ['a', 'b'],
'A_2': ['aa', 'bb'], 'A_3': ['aaa', 'bbb'],
'A_person': ['Mike', 'Amy']})
pd.wide_to_long(df, stubnames='A_', i='id', j='num')
# A_person A_
#id num
#1 1 Mike a
#2 1 Amy b
#1 2 Mike aa
#2 2 Amy bb
#1 3 Mike aaa
#2 3 Amy bbb
由于默认行为仅考虑数字,因此'A_person'
被忽略。如果您想将其添加到转换中,则可以使用 suffix
参数。让我们告诉它我们想要数字或单词。
pd.wide_to_long(df, stubnames='A_', i='id', j='suffix', suffix='(\d+|\w+)')
# A_
#id suffix
#1 1 a
#2 1 b
#1 2 aa
#2 2 bb
#1 3 aaa
#2 3 bbb
#1 person Mike
#2 person Amy
现在,如果您的 df 开头没有数字后缀,您也可以使用 suffix 参数来解决这个问题。默认调用将会失败,因为它需要数字,但告诉它查找单词会给您带来您想要的结果。
df = pd.DataFrame({'id': [1,2], 'A_one': ['a', 'b'],
'A_two': ['aa', 'bb'], 'A_three': ['aaa', 'bbb'],
'A_person': ['Mike', 'Amy']})
pd.wide_to_long(df, stubnames='A_', i='id', j='num')
#Empty DataFrame
#Columns: [A_three, A_person, A_one, A_two, A_]
#Index: []
pd.wide_to_long(df, stubnames='A_', i='id', j='suffix', suffix='\w+')
# A_
#id suffix
#1 one a
#2 one b
#1 person Mike
#2 person Amy
#1 three aaa
#2 three bbb
#1 two aa
#2 two bb
如果您不想包含 A_person
,您可以告诉后缀参数仅包含某些 stub 。
pd.wide_to_long(df, stubnames='A_', i='id', j='num', suffix='(one|two|three)')
# A_person A_
#id num
#1 one Mike a
#2 one Amy b
#1 three Mike aaa
#2 three Amy bbb
#1 two Mike aa
#2 two Amy bb
基本上,如果您可以使用正则表达式捕获它,则可以将其传递给后缀以仅使用您想要的列。
关于python - pandas Wide_to_long 后缀参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50160774/
这似乎是一个愚蠢的问题,但为什么在许多语言中存在 ++ 和 -- 运算符的前缀和后缀版本,但是没有类似 += 或 -= 等其他运算符的前缀/后缀版本?例如,如果我能写出这段代码: myArray[x+
我需要链接到第三方预建共享库。在 Windows 上,这意味着与 Third_party.lib 链接,在 Linux/Android 上,这意味着与 libThird_party.so 链接。所以为
我正在学习 C++ 中的运算符重载。原始后缀++ 的特性是它的优先级低于赋值运算符。例如,int i=0, j=0; i=j++; cout using namespace std; class V
如何在文本区域中添加每行前缀.. 示例: 这是文本区域的内容: hello124 我想为每一行添加一个 [b] 前缀和后缀,这样当我点击一个按钮时,结果将是: [b]hello[/b] [b]124[
背景:在传统的逆波兰表示法中,所有运算符都必须具有固定长度,这使得 RPN 可以很容易地被代码评估和操作,因为每个标记、表达式和子表达式都是“自包含”的,以至于人们可以盲目地替换 y在 x y *为
我有以下旨在修改日期格式的Javascript,但是我想添加原始日期或后缀,例如“st”,“nd”,“rd”,“th”到每个结束日期编号。例如,假设我们当前的日期设置为 4 月 28 日,但我想将日期
我想制定一个 header 检查规则来添加回复并将“发件人”更改为“不回复”。我将它用于某种扩散列表地址 我试过这个正则表达式代码,但它不起作用: if !/^From:(.+@myserver\.f
我想改变数据框的列内容,以便单元格内容以列名作为前缀: > x x VarX VarY 1 A C 2 B D 3 A C 4 B D > x$V
当我执行 ipconfig/all 时,我看到 DNS 后缀搜索列表。我需要从 java 中检索该值。有谁知道如何获得它或它从哪里来? 最佳答案 DNS 后缀列表读取自 HKLM\SYSTEM\Cur
当您编写一个包含大量类的应用程序时,您是否为类名使用了前缀/后缀?还是我应该只依赖我已经定义的 namespace ? 在我的例子中,我有这样的类: Blur Contrast Sharpen Inv
大多数浏览器会像这样显示有序列表: 1. foo 2. bar 3. baz 有没有办法更改编号以改为使用前缀: #1 foo #2 bar #3 baz 最佳答案 这是我能想到的最好的,你只在 Fi
我需要批量重命名多个图像,并希望使用父目录作为基本名称。为防止一个覆盖另一个,必须添加后缀。重命名过程的顺序必须遵循文件的时间戳。因为“第一个”文件是我正在使用它的网站的特色图片。 树: └── ma
我试图使用 sed 替换文件中的一些字符串,但遇到了一个问题。 我有以下字符串: TEMPLATE_MODULE TEMPLATE_SOME_ERR TEMPLATE_MORE_ERR 我想用一些字符
我对后缀/前缀运算符的优先级 和关联性 感到困惑。 一方面,当我阅读 K&R 书时,它指出: (*ip)++ The parentheses are necessary in this last ex
我有一个具有以下结构的图 V = {A1, A2, A3, A4, A5, .., An} E = {E1, E2, E3, E4, .., Ek} 现在我们定义A1的后缀: S(A1) = {All
这是解释性代码。语言是Java,代码使用Android。 fg.setTextSize(height*0.50f); //<-'f' is in the brackets 或 @Override pr
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 9 年前。 Improve this
我正在编写自己的数组类作为练习。因为,我读到非成员函数实际上在某些方面比成员函数更好。( Scott Meyers ) 我正在尝试编写尽可能多的运算符重载作为非成员函数。运算符重载 + , - 作为非
谁能解释一下关于 C 编程语言的中缀、后缀和前缀表示法是什么? 最佳答案 这是对 three terms, and how they apply 的一个很好的讨论. C 语言几乎到处都使用中缀表示法。
我有这种情况:我需要在输入文本字段 (html) 中添加引号而不更改输入的值。我正在使用 angular,所以我使用 ngModel,它看起来像这样 我希望输入字段显示“{{data}} 中的任何内
我是一名优秀的程序员,十分优秀!