简体   繁体   English

从特定字符开始逐行提取元素

[英]Extract element by line starting with a specific character

I'm currently working on this DataFrame python :我目前正在研究这个 DataFrame python :
提取数据框

The data-set has one column and n lines.数据集有 1 列和 n 行。

I would like to extract specifics components of specifics line, for exemple :我想提取特定行的特定组件,例如:

For each line i starting with 'n', store in variable x the second element of the line i.对于以“n”开头的每一行 i,将行 i 的第二个元素存储在变量 x 中。

or或者

For each line i starting with 'e', store in variable x the second and third element of the line i.对于以“e”开头的每一行 i,将行 i 的第二个和第三个元素存储在变量 x 中。

I would like to know which function/operation I can use for this problem.我想知道我可以使用哪个函数/操作来解决这个问题。

Create simple example:创建简单示例:

d = pd.DataFrame({'a': ['aaaak', 'k jhs', 'anhdga', 'kjdhs']})

You can use column.str and see a first letter:您可以使用 column.str 并查看第一个字母:

data.a.str[0]

out:出去:

0    a
1    k
2    a
3    k

And you can check what the letter is:你可以检查这封信是什么:

data.a.str[0] == 'a'

out:出去:

0     True
1    False
2     True
3    False

You can call raws with only first letter 'a':你可以只用第一个字母 'a' 调用 raws:

data[data.a.str[0] == 'a']

out:出去:

        a
0   aaaak
2  anhdga

And then you can get another letter in raws which started from 'a':然后你可以得到另一个从 'a' 开始的 raws 字母:

data[data.a.str[0] == 'a'].a.str[2]

out:出去:

0    a
2    h

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM