How to apply a regex to a data frame column?
import pandas as pd
df = pd.DataFrame({'col1': ['negative', 'positive', 'neutral', 'neutral', 'positive']})
cdict = {'n.*': -1, 'p.*': 0}
df['col2'] = df['col1'].map(cdict)
print(df.head())
Current output is:
: col1 col2
: 0 negative NaN
: 1 positive NaN
: 2 neutral NaN
: 3 neutral NaN
: 4 positive NaN
But expected results:
: col1 col2
: 0 negative -1
: 1 positive 1
: 2 neutral -1
: 3 neutral -1
: 4 positive 1
cdict = {'n.*': -1, 'p.*': 1}for your expected output, I assume its a typo