Home > Back-end >  Extracting certain words from column
Extracting certain words from column

Time:12-23

I have csv file like below in a column 'filename' and I would like to have the label column name only the word before underscore (_)

For example: output like below

filename                                                      label

How can I extract only those words from following column and paste into label column?

CodePudding user response:

Using str.split() method, and assuming that you import your csv as a Dataframe:

import pandas as pd
df = pd.read_csv('file_path')
df['label'] = df['filename'].str.split('____').str[0]

CodePudding user response:

maybe you can try it


import padndas as pd

data = pd.read_csv('file_path')

def extract_label(filename):
    rerurn filename.split('____')[0]

data['label'] = data['filename'].apply(lambda filename:extract_label(filename))

CodePudding user response:

df['label'] = df.apply(lambda row:row['filename'].split('____')[0], axis=1)

CodePudding user response:

Please try this line on your code.

df['Label'] = [x.split("___")[0] for x in df['filename']]
  • Related