Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在pandas中的某些字符之前选择部分字符串名称_Python_Pandas_String_Slice - Fatal编程技术网

Python 在pandas中的某些字符之前选择部分字符串名称

Python 在pandas中的某些字符之前选择部分字符串名称,python,pandas,string,slice,Python,Pandas,String,Slice,我有很多列名称,格式如下: A-1_45_GG___________________LL B_2_45_GLKK___________________KK 我正在寻找一种在长下划线字符之前选择字符串名称的通用方法。因此,我想要的输出将被重命名为[A-1_45_GG,B_2_45_GL]。在Python中如何实现这一点?如果您的名字中没有双下划线,您可以将其拆分为“\uuuuuuu”,然后使用第一个索引 string = "A-1_45_GG___________________LL&

我有很多列名称,格式如下:

A-1_45_GG___________________LL
B_2_45_GLKK___________________KK

我正在寻找一种在长下划线字符之前选择字符串名称的通用方法。因此,我想要的输出将被重命名为
[A-1_45_GG,B_2_45_GL]
。在Python中如何实现这一点?

如果您的名字中没有双下划线,您可以将其拆分为“\uuuuuuu”,然后使用第一个索引

string = "A-1_45_GG___________________LL"
string.split("__")[0]
输出:

'A-1_45_GG'
yourlist = ["A-1_45_GG___________________LL", "B_2_45_GLKK___________________KK"]

print(list(map(lambda x: x.split("__")[0], yourlist)))
['A-1_45_GG', 'B_2_45_GLKK']
['A-1_45_GG', 'B_2_45_GLKK']
如果要使用列表:

'A-1_45_GG'
yourlist = ["A-1_45_GG___________________LL", "B_2_45_GLKK___________________KK"]

print(list(map(lambda x: x.split("__")[0], yourlist)))
['A-1_45_GG', 'B_2_45_GLKK']
['A-1_45_GG', 'B_2_45_GLKK']
输出:

'A-1_45_GG'
yourlist = ["A-1_45_GG___________________LL", "B_2_45_GLKK___________________KK"]

print(list(map(lambda x: x.split("__")[0], yourlist)))
['A-1_45_GG', 'B_2_45_GLKK']
['A-1_45_GG', 'B_2_45_GLKK']

如果你的名字中没有双下划线,你可以把它除以“\uuuuuuuuuuuuuuuuuu”,然后取第一个索引

string = "A-1_45_GG___________________LL"
string.split("__")[0]
输出:

'A-1_45_GG'
yourlist = ["A-1_45_GG___________________LL", "B_2_45_GLKK___________________KK"]

print(list(map(lambda x: x.split("__")[0], yourlist)))
['A-1_45_GG', 'B_2_45_GLKK']
['A-1_45_GG', 'B_2_45_GLKK']
如果要使用列表:

'A-1_45_GG'
yourlist = ["A-1_45_GG___________________LL", "B_2_45_GLKK___________________KK"]

print(list(map(lambda x: x.split("__")[0], yourlist)))
['A-1_45_GG', 'B_2_45_GLKK']
['A-1_45_GG', 'B_2_45_GLKK']
输出:

'A-1_45_GG'
yourlist = ["A-1_45_GG___________________LL", "B_2_45_GLKK___________________KK"]

print(list(map(lambda x: x.split("__")[0], yourlist)))
['A-1_45_GG', 'B_2_45_GLKK']
['A-1_45_GG', 'B_2_45_GLKK']

另一种方法是通过正则表达式拆分字符串

import re

lst = ["A-1_45_GG___________________LL", "B_2_45_GLKK___________________KK"]

print([re.split("[_]{2,}", i)[0] for i in lst])
输出:


另一种方法是通过正则表达式拆分字符串

import re

lst = ["A-1_45_GG___________________LL", "B_2_45_GLKK___________________KK"]

print([re.split("[_]{2,}", i)[0] for i in lst])
输出: