Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.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 熊猫基于str.find作为开始和停止位置对字符串进行切片_Python_Pandas - Fatal编程技术网

Python 熊猫基于str.find作为开始和停止位置对字符串进行切片

Python 熊猫基于str.find作为开始和停止位置对字符串进行切片,python,pandas,Python,Pandas,我有一个像这样的数据框 commands *client interface : Eth-Trunk45.2903 is up *client interface : Eth-Trunk46.2620 is up *client interface : Eth-Trunk46.2988 is up *client interface : Eth-Trunk55.1703 is up *client interface : Eth-Tru

我有一个像这样的数据框

commands
*client interface       : Eth-Trunk45.2903 is up
*client interface       : Eth-Trunk46.2620 is up
*client interface       : Eth-Trunk46.2988 is up
*client interface       : Eth-Trunk55.1703 is up
*client interface       : Eth-Trunk55.1704 is up
*client interface       : GigabitEthernet4/1/12.102 is up
如何切片字符串并获得如下输出

commands
Eth-Trunk45.2903
Eth-Trunk46.2620
Eth-Trunk46.2988
Eth-Trunk55.1703
Eth-Trunk55.1704
GigabitEthernet4/1/12.102
我试着

但这只会给我带来价值

请提供帮助。

用于获取值之间的值:

df['commands'] = df['commands'].str.extract(r":(.+) is", expand=False)
print (df)
                     commands
0            Eth-Trunk45.2903
1            Eth-Trunk46.2620
2            Eth-Trunk46.2988
3            Eth-Trunk55.1703
4            Eth-Trunk55.1704
5   GigabitEthernet4/1/12.102
您的解决方案在中是可行的,因为pandas切片仅对所有列使用相同的整数:

df['commands'] = df['commands'].apply(lambda x: x[x.find(': ') + 1: x.find(' is ')])
print (df)
                     commands
0            Eth-Trunk45.2903
1            Eth-Trunk46.2620
2            Eth-Trunk46.2988
3            Eth-Trunk55.1703
4            Eth-Trunk55.1704
5   GigabitEthernet4/1/12.102

df['commands'] = df['commands'].apply(lambda x: x[x.find(': ') + 1: x.find(' is ')])
print (df)
                     commands
0            Eth-Trunk45.2903
1            Eth-Trunk46.2620
2            Eth-Trunk46.2988
3            Eth-Trunk55.1703
4            Eth-Trunk55.1704
5   GigabitEthernet4/1/12.102
print (df['commands'].str.slice(26, 42))
0    Eth-Trunk45.2903
1    Eth-Trunk46.2620
2    Eth-Trunk46.2988
3    Eth-Trunk55.1703
4    Eth-Trunk55.1704
5    GigabitEthernet4
Name: commands, dtype: object