lundi 31 mai 2021

Perform an action from a condition for a Python Dataframe

I have the following dataframe:

import pandas as pd
import re
df = pd.DataFrame ({'example': ['ACETATO MOLOCUATO']})

The condition is that if the string ends in "ATO" I will choose only the first three words. The way I planted it is as follows:

#If the example ends in "ATO"

df ['example']. str.contains ('ATO $')

#Then Extract the first 3 words

df ['example']. str.extract ('(\ w +) \ s (\ w +) \ s (\ w +)')

#Otherwise, print the dataframe without changes

I would like to know the best way to relate this last action based on the first condition.

Aucun commentaire:

Enregistrer un commentaire