如何在一列中搜索数字/字符串并在不同的列中相应地填充数字?

问题描述 投票:0回答:2

我正在注释击键记录数据(在 Excel 中),我需要为每个记录的键(输入列)创建一个试用编号(试用列),具体取决于每行开头记录的编号(在 RETURN 键之后)。如下表所示,对于“10.red”这一行,每个键的试用号应该是10.

谁能建议一种自动填写试用栏的方法?即使它不是 100% 准确,它也会为我节省很多时间。提前致谢!

输入 试用
退货 9
1 10
0 10
. 10
r 10
e 10
d 10
退货 10
1 11
1 11
. 11
b 11
l 11
u 11
e 11
退货 11
1 12
2 12

我想 python 可以提供帮助,但我不知道到底有什么功能。

python r autofill
2个回答
0
投票
import pandas as pd

s = 'Input\tTrial\nRETURN\t9\n1\t10\n0\t10\n.\t10\nr\t10\ne\t10\nd\t10\nRETURN\t10\n1\t11\n1\t11\n.\t11\nb\t11\nl\t11\nu\t11\ne\t11\nRETURN\t11\n1\t12\n2\t12'
data = [r.split('\t')[0] for r in s.split('\n')[1:]]
df = pd.DataFrame(data, columns=['Input'])
start_trial = 9
df['Trial'] = start_trial
df.loc[1:, 'Trial'] += ((df.Input == 'RETURN').cumsum())[1:]
print(df)

版画

     Input  Trial
0   RETURN      9
1        1     10
2        0     10
3        .     10
4        r     10
5        e     10
6        d     10
7   RETURN     11
8        1     11
9        1     11
10       .     11
11       b     11
12       l     11
13       u     11
14       e     11
15  RETURN     12
16       1     12
17       2     12

0
投票

带底座 R:

dt <- structure(list(V1 = c("RETURN", "1", "0", ".", "r", "e", "d", 
                            "RETURN", "1", "1", ".", "b", "l", "u", "e", "RETURN", "1", "2"
), V2 = c(9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 
          11L, 11L, 11L, 11L, 11L, 12L, 12L)), class = "data.frame", row.names = c(NA, 
                                                                                   -18L))

dt$V3 <- 9 + cumsum(dt$V1 == "RETURN") - as.numeric(dt$V1 == "RETURN")
dt
#>        V1 V2 V3
#> 1  RETURN  9  9
#> 2       1 10 10
#> 3       0 10 10
#> 4       . 10 10
#> 5       r 10 10
#> 6       e 10 10
#> 7       d 10 10
#> 8  RETURN 10 10
#> 9       1 11 11
#> 10      1 11 11
#> 11      . 11 11
#> 12      b 11 11
#> 13      l 11 11
#> 14      u 11 11
#> 15      e 11 11
#> 16 RETURN 11 11
#> 17      1 12 12
#> 18      2 12 12

创建于 2023-05-18 与 reprex v2.0.2

在 Excel 中,在第一行下方使用 IF:

© www.soinside.com 2019 - 2024. All rights reserved.