简体   繁体   English

从python中的列中提取Word匹配

[英]Extract Word match from column in python

I have a data frame in which one of the column is of the form我有一个数据框,其中一列的形式为

Mat_Headers_Turbine = df_2['1'].unique().tolist()
print(Mat_Headers_Turbine)

['TURBINE , GAS ', 'TURBINE ', 'TURBINE,STEAM ', 'TURBINE, STEAM ', 'TURBINE,EXPANSION ', 'TURBINE STEAM ', 'STEAM TURBINE ', 'TURATING ', 'PUMPS, RECIPROCATING ', 'BLOWERS ', 'REGENERATOR CYLONE SEPERATOR ', 'MOBILE CRANE ', 'MECHANICAL SEAL ', 'TOOLS - MISCELLANEOUS', 'LADDERS ', 'TRANSDUCER ', 'FLAME SCANNERS AND PARTS:', 'VALVE, CONTROL, GLOBE ']

I only need to extract the corresponding value to "TURBINE" which is "gas/steam/air/engine/expansion into a new column. The no. of rows are around 50,000. How can i do this ?我只需要提取“TURBINE”的相应值,即“gas/steam/air/engine/expansion into a new column。行数约为50,000。我该怎么做?

You only need to use the following pandas query您只需要使用以下熊猫查询

turbine_values = df_2[(df_2['1'].notnull()) & (df_2['1'].str.contains('TURBINE'))]['1'].apply(lambda turbine_string: turbine_string.split(',')[-1].strip())

This will bring all rows that contains 'TURBINE' on the desired column and then the corresponding value.这将在所需列上显示包含'TURBINE'所有行,然后是相应的值。

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

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