3

I have been researching similar questions but have not been able to find answers. I would appreciate if you could help me since I am new to programming and Python(2.7)..

So I have this panda dataframe.

This is a data I have: enter image description here

What I would like to do is that putting 1 in a man column, if a "man" string is contained in info column( and in the same row). Otherwise, I would like to put 0. The same for woman column. So, what I want is something like this.

This is what I want to create: enter image description here

Is there any way I can create a function which identifies specified string, such as man or woman in info column, and puts 1 or 0 in man and woman columns accordingly?

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
yusuke0426
  • 273
  • 2
  • 4
  • 15

2 Answers2

1

New Answer

dataframe = pd.DataFrame([['Age is 83,sex is man'],
                          ['sex is woman,age is 74']],
                         columns=['info'])

mw = dataframe['info'].str.extract(r'sex is (woman|man)', expand=False)
pd.concat([dataframe, pd.get_dummies(mw).astype(int)], axis=1)

Old Answer

dataframe['man'] = dataframe['info'].str.match(r'^.*sex is man.*$', re.I).astype(int)
dataframe['woman'] = dataframe['info'].str.match(r'^.*sex is woman.*$', re.I).astype(int)
dataframe

enter image description here

you might also find this interesting.

Community
  • 1
  • 1
piRSquared
  • 285,575
  • 57
  • 475
  • 624
0

This works

import string
df['woman'] = df['info'].map(lambda x: x.translate(None, string.punctuation)).map(lambda x: 1 if 'woman' in x.lower().split() else 0)
df['man'] = df['info'].map(lambda x: x.translate(None, string.punctuation)).map(lambda x: 1 if 'man' in x.lower().split() else 0)
df

the output

HIM
  • 1
  • 2