简体   繁体   中英

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 ?

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.

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