简体   繁体   English

如何删除 pandas 中字符前面的所有字符串值?

[英]How to remove all string values that precede a character in pandas?

I have the following dataframe:我有以下 dataframe:

data = {'Name':['Square_Train_1', 'Stims1/Neut/32Neut1.jpg', 'Square_Train_2',
       'Stims1/Neg/114Neg1.jpg', 'Square_Train_3',
       'Stims1/Pos/129Pos1.jpg', 'Stims1/Neut/58Neut1.jpg',
       'Stims1/Neg/13Neg1.jpg', 'Stims1/Pos/5Pos1.jpg',
       'Stims1/Pos/25Pos1.jpg', 'Stims1/Neg/47Neg1.jpg',
       'Stims1/Neut/8Neut1.jpg', 'Stims1/Neg/129Neg1.jpg',
       'Stims1/Neut/42Neut1.jpg', 'Stims1/Pos/98Pos1.jpg',
       'Stims1/Neut/24Neut1.jpg', 'Stims1/Neg/6Neg1.jpg',
       'Stims1/Pos/107Pos1.jpg', 'Stims1/Neg/100Neg1.jpg',
       'Stims1/Pos/77Pos1.jpg', 'Stims1/Neut/3Neut1.jpg',
       'Stims1/Neg/53Neg1.jpg', 'Stims1/Pos/157Pos1.jpg',
       'Stims1/Neut/13Neut1.jpg', 'Stims1/Neut/9Neut1.jpg',
       'Stims1/Pos/104Pos1.jpg', 'Stims1/Neg/64Neg1.jpg',
       'Stims1/Neut/30Neut1.jpg', 'Stims1/Pos/43Pos1.jpg',
       'Stims1/Neg/1Neg1.jpg', 'Stims1/Neut/59Neut1.jpg',
       'Stims1/Neg/172Neg1.jpg', 'Stims1/Pos/56Pos1.jpg',
       'Stims1/Pos/44Pos1.jpg', 'Stims1/Neg/34Neg1.jpg',
       'Stims1/Neut/16Neut1.jpg', 'Stims1/Neut/47Neut1.jpg',
       'Stims1/Neg/21Neg1.jpg', 'Stims1/Pos/96Pos1.jpg',
       'Stims1/Neg/50Neg1.jpg', 'Stims1/Pos/2Pos1.jpg',
       'Stims1/Neut/21Neut1.jpg', 'Stims1/Neg/65Neg1.jpg',
       'Stims1/Pos/35Pos1.jpg', 'Stims1/Neut/51Neut1.jpg',
       'Stims1/Neut/55Neut1.jpg', 'Stims1/Pos/60Pos1.jpg',
       'Stims1/Neg/30Neg1.jpg', 'Stims1/Neut/7Neut1.jpg',
       'Stims1/Pos/9Pos1.jpg', 'Stims1/Neg/41Neg1.jpg',
       'Stims1/Pos/31Pos1.jpg', 'Stims1/Neut/40Neut1.jpg',
       'Stims1/Neg/156Neg1.jpg', 'Stims1/Neg/135Neg1.jpg',
       'Stims1/Pos/71Pos1.jpg', 'Stims1/Neut/26Neut1.jpg',
       'Stims1/Pos/105Pos1.jpg', 'Stims1/Neg/17Neg1.jpg',
       'Stims1/Neut/44Neut1.jpg', 'Stims1/Pos/150Pos1.jpg',
       'Stims1/Neut/57Neut1.jpg', 'Stims1/Neg/12Neg1.jpg',
       'Stims1/Pos/24Pos1.jpg', 'Stims1/Neg/131Neg1.jpg',
       'Stims1/Neut/31Neut1.jpg', 'Stims1/Pos/10Pos1.jpg',
       'Stims1/Neut/11Neut1.jpg', 'Stims1/Neg/118Neg1.jpg',
       'Stims1/Neg/51Neg1.jpg', 'Stims1/Pos/48Pos1.jpg',
       'Stims1/Neut/34Neut1.jpg', 'Stims1/Pos/148Pos1.jpg',
       'Stims1/Neut/22Neut1.jpg', 'Stims1/Neg/176Neg1.jpg',
       'Stims1/Neut/5Neut1.jpg', 'Stims1/Neg/104Neg1.jpg',
       'Stims1/Pos/68Pos1.jpg', 'Stims1/Neut/35Neut1.jpg',
       'Stims1/Pos/14Pos1.jpg', 'Stims1/Neg/136Neg1.jpg',
       'Stims1/Neut/54Neut1.jpg', 'Stims1/Neg/107Neg1.jpg',
       'Stims1/Pos/47Pos1.jpg', 'Stims1/Neut/43Neut1.jpg',
       'Stims1/Neg/58Neg1.jpg', 'Stims1/Pos/20Pos1.jpg',
       'Stims1/Neut/6Neut1.jpg', 'Stims1/Neg/63Neg1.jpg',
       'Stims1/Pos/135Pos1.jpg', 'Stims1/Neut/39Neut1.jpg',
       'Stims1/Neg/164Neg1.jpg', 'Stims1/Pos/125Pos1.jpg',
       'Stims1/Neg/117Neg1.jpg', 'Stims1/Neut/48Neut1.jpg',
       'Stims1/Pos/69Pos1.jpg', 'Stims1/Pos/37Pos1.jpg',
       'Stims1/Neg/159Neg1.jpg', 'Stims1/Neut/36Neut1.jpg',
       'Stims1/Pos/75Pos1.jpg', 'Stims1/Neg/180Neg1.jpg',
       'Stims1/Neut/50Neut1.jpg', 'Stims1/Neg/7Neg1.jpg',
       'Stims1/Pos/11Pos1.jpg', 'Stims1/Neut/52Neut1.jpg',
       'Stims1/Pos/29Pos1.jpg', 'Stims1/Neut/46Neut1.jpg',
       'Stims1/Neg/115Neg1.jpg', 'Stims1/Neg/31Neg1.jpg',
       'Stims1/Pos/66Pos1.jpg', 'Stims1/Neut/14Neut1.jpg',
       'Stims1/Neut/53Neut1.jpg', 'Stims1/Neg/162Neg1.jpg',
       'Stims1/Pos/97Pos1.jpg', 'Stims1/Neg/35Neg1.jpg',
       'Stims1/Neut/45Neut1.jpg', 'Stims1/Pos/32Pos1.jpg',
       'Stims1/Pos/81Pos1.jpg', 'Stims1/Neg/24Neg1.jpg',
       'Stims1/Neut/1Neut1.jpg', 'Stims1/Neut/20Neut1.jpg',
       'Stims1/Neg/69Neg1.jpg', 'Stims1/Pos/52Pos1.jpg',
       'Stims2/Pos/35Pos2.jpg', 'Stims2/Neut/1Neut2.jpg',
       'Stims2/Neg/30Neg2.jpg', 'Stims2/Neg/156Neg2.jpg',
       'Stims2/Neut/59Neut2.jpg', 'Stims2/Pos/150Pos2.jpg',
       'Stims2/Neg/114Neg2.jpg', 'Stims2/Neut/39Neut2.jpg',
       'Stims2/Pos/98Pos2.jpg', 'Stims2/Pos/14Pos2.jpg',
       'Stims2/Neg/24Neg2.jpg', 'Stims2/Neut/51Neut2.jpg',
       'Stims2/Pos/48Pos2.jpg', 'Stims2/Neg/31Neg2.jpg',
       'Stims2/Neut/26Neut2.jpg', 'Stims2/Neg/35Neg2.jpg',
       'Stims2/Neut/40Neut2.jpg', 'Stims2/Pos/60Pos2.jpg',
       'Stims2/Pos/77Pos2.jpg', 'Stims2/Neut/9Neut2.jpg',
       'Stims2/Neg/47Neg2.jpg', 'Stims2/Neg/107Neg2.jpg',
       'Stims2/Pos/66Pos2.jpg', 'Stims2/Neut/55Neut2.jpg',
       'Stims2/Neut/14Neut2.jpg', 'Stims2/Pos/56Pos2.jpg',
       'Stims2/Neg/34Neg2.jpg', 'Stims2/Neg/131Neg2.jpg',
       'Stims2/Pos/97Pos2.jpg', 'Stims2/Neut/52Neut2.jpg',
       'Stims2/Neut/45Neut2.jpg', 'Stims2/Neg/162Neg2.jpg',
       'Stims2/Pos/129Pos2.jpg', 'Stims2/Pos/52Pos2.jpg',
       'Stims2/Neg/104Neg2.jpg', 'Stims2/Neut/48Neut2.jpg',
       'Stims2/Neut/21Neut2.jpg', 'Stims2/Pos/104Pos2.jpg',
       'Stims2/Neg/50Neg2.jpg', 'Stims2/Pos/24Pos2.jpg',
       'Stims2/Neut/34Neut2.jpg', 'Stims2/Neg/176Neg2.jpg',
       'Stims2/Neg/129Neg2.jpg', 'Stims2/Pos/47Pos2.jpg',
       'Stims2/Neut/36Neut2.jpg', 'Stims2/Pos/157Pos2.jpg',
       'Stims2/Neg/58Neg2.jpg', 'Stims2/Neut/7Neut2.jpg',
       'Stims2/Neut/53Neut2.jpg', 'Stims2/Pos/69Pos2.jpg',
       'Stims2/Neg/172Neg2.jpg', 'Stims2/Pos/68Pos2.jpg',
       'Stims2/Neut/35Neut2.jpg', 'Stims2/Neg/100Neg2.jpg',
       'Stims2/Neg/17Neg2.jpg', 'Stims2/Pos/148Pos2.jpg',
       'Stims2/Neut/46Neut2.jpg', 'Stims2/Neut/16Neut2.jpg',
       'Stims2/Pos/105Pos2.jpg', 'Stims2/Neg/159Neg2.jpg',
       'Stims2/Pos/29Pos2.jpg', 'Stims2/Neg/64Neg2.jpg',
       'Stims2/Neut/58Neut2.jpg', 'Stims2/Neut/30Neut2.jpg',
       'Stims2/Pos/71Pos2.jpg', 'Stims2/Neg/41Neg2.jpg',
       'Stims2/Neut/20Neut2.jpg', 'Stims2/Neg/69Neg2.jpg',
       'Stims2/Pos/9Pos2.jpg', 'Stims2/Pos/5Pos2.jpg',
       'Stims2/Neut/13Neut2.jpg', 'Stims2/Neg/1Neg2.jpg',
       'Stims2/Pos/31Pos2.jpg', 'Stims2/Neg/21Neg2.jpg',
       'Stims2/Neut/32Neut2.jpg', 'Stims2/Pos/96Pos2.jpg',
       'Stims2/Neg/118Neg2.jpg', 'Stims2/Neut/57Neut2.jpg',
       'Stims2/Neut/3Neut2.jpg', 'Stims2/Pos/32Pos2.jpg',
       'Stims2/Neg/117Neg2.jpg', 'Stims2/Neg/6Neg2.jpg',
       'Stims2/Pos/10Pos2.jpg', 'Stims2/Neut/44Neut2.jpg',
       'Stims2/Pos/25Pos2.jpg', 'Stims2/Neut/50Neut2.jpg',
       'Stims2/Neg/51Neg2.jpg', 'Stims2/Neut/47Neut2.jpg',
       'Stims2/Neg/135Neg2.jpg', 'Stims2/Pos/125Pos2.jpg',
       'Stims2/Neut/43Neut2.jpg', 'Stims2/Neg/7Neg2.jpg',
       'Stims2/Pos/11Pos2.jpg', 'Stims2/Neut/22Neut2.jpg',
       'Stims2/Pos/20Pos2.jpg', 'Stims2/Neg/180Neg2.jpg',
       'Stims2/Neut/31Neut2.jpg', 'Stims2/Neg/164Neg2.jpg',
       'Stims2/Pos/37Pos2.jpg', 'Stims2/Neg/13Neg2.jpg',
       'Stims2/Neut/5Neut2.jpg', 'Stims2/Pos/135Pos2.jpg',
       'Stims2/Neg/53Neg2.jpg', 'Stims2/Neut/54Neut2.jpg',
       'Stims2/Pos/81Pos2.jpg', 'Stims2/Pos/44Pos2.jpg',
       'Stims2/Neut/11Neut2.jpg', 'Stims2/Neg/115Neg2.jpg',
       'Stims2/Neut/6Neut2.jpg', 'Stims2/Pos/107Pos2.jpg',
       'Stims2/Neg/136Neg2.jpg', 'Stims2/Pos/75Pos2.jpg',
       'Stims2/Neg/65Neg2.jpg', 'Stims2/Neut/42Neut2.jpg',
       'Stims2/Pos/43Pos2.jpg', 'Stims2/Neut/24Neut2.jpg',
       'Stims2/Neg/12Neg2.jpg', 'Stims2/Neut/8Neut2.jpg',
       'Stims2/Pos/2Pos2.jpg', 'Stims2/Neg/63Neg2.jpg']} 
  
# Create DataFrame 
df = pd.DataFrame(data) 
  
# Print the output. 
df 

The goal is to remove all characters that precede the '/' character.目标是删除“/”字符之前的所有字符。

I tried 'lstrip':我试过'lstrip':

df['Name'] = df['Name'].map(lambda x: x.lstrip('Stims1/Neut/'))
df['Name'] = df['Name'].map(lambda x: x.lstrip('Stims1/Pos/'))
df['Name'] = df['Name'].map(lambda x: x.lstrip('Pos/'))
df['Name'] = df['Name'].map(lambda x: x.lstrip('2'))
df['Name'] = df['Name'].map(lambda x: x.lstrip('/Pos/'))
df['Name'] = df['Name'].map(lambda x: x.lstrip('Neg/'))
df['Name'] = df['Name'].map(lambda x: x.lstrip('/Neut/'))
df['Name'] = df['Name'].map(lambda x: x.lstrip('ut/'))

The problem with lstrip is that it requires a lot of different inputs for matching the string, and often strips too many strings. lstrip 的问题在于它需要很多不同的输入来匹配字符串,并且经常会剥离太多的字符串。

I would like to avoid using 'replace', as it is even less efficient;我想避免使用“替换”,因为它的效率更低; it requires entering every single combination of strings.它需要输入字符串的每一个组合。 The same problem seems to apply to 're'.同样的问题似乎也适用于're'。

Is there a way to remove all characters that precede the '/' most panefficiently?有没有办法最有效地删除“/”之前的所有字符?

What it really looks like you're trying to do is grab just the filename and drop the rest of the directory from the filepath.你真正想要做的只是抓住文件名并从文件路径中删除目录的 rest 。 If that is the case, I would use df.apply with os.path.basename如果是这种情况,我会将df.applyos.path.basename一起使用

>>> import os
>>> df['Name'] = df['Name'].apply(os.path.basename)

Which results in这导致

>>> df
               Name
0    Square_Train_1
1       32Neut1.jpg
2    Square_Train_2
3       114Neg1.jpg
4    Square_Train_3
..              ...
238     24Neut2.jpg
239      12Neg2.jpg
240      8Neut2.jpg
241       2Pos2.jpg
242      63Neg2.jpg

[243 rows x 1 columns]

暂无
暂无

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

相关问题 对于 pandas 中的列中的所有值,如何从字符串中删除字符并将 rest 转换为 integer 或十进制? - How to remove a character from a string and convert the rest into integer or decimal, for all values in a column in pandas? 如何删除 Pandas 中某个字符后的所有字符串或数字? - How to remove all string or numbers after certain Character in Pandas? Pandas删除字符后的列中的所有字符串 - Pandas remove all of a string in a column after a character 如何删除 Pandas Dataframe 列中所有值的字符串的最后一个字符? - How to delete the last character of a string for all values in a Pandas Dataframe column? 如何删除字符串中字符左侧的所有字符 - How to remove all characters to the left of a character in a string 有没有办法从由字符串组成的列中的所有值中删除字符串字符? - Is there a way to remove a string character from all the values in a column consisting of strings? 如何及时找到所有那些在对应的 X 元素之前的 Y 元素? - Pandas, Python - How to find all those Y elements that precede the corresponding X elements in time? - Pandas, Python 如何从数字数据中删除除“。”之外的所有字符串值和“-”使用 pandas - How to remove all string values from numerical data except "." and "-" using pandas 如果 Pandas 中字符串的开头和结尾处可用,如何有效地删除字符? - How to effeciently remove character if available at the beginning and end of a string in Pandas? 如何在熊猫列中的特殊字符之前删除字符串的一部分? - How to remove part of string ahead of special character in a column in Pandas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM