简体   繁体   中英

Extract element by line starting with a specific character

I'm currently working on this DataFrame python :
提取数据框

The data-set has one column and n lines.

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.

or

For each line i starting with 'e', store in variable x the second and third element of the line i.

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:

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':

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':

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

out:

0    a
2    h

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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