-2

I have a csv file few columns are as Yes/No's I have tried most of the solutions on stack over flow nothing seems to be working.

sample['colval'] = sample['colval'].apply(lambda x: 0 if x=='N' else 1)

For example, I used the above code on my data. It converted everything to 1's, as in Y to 1 and N to 1.

So many other examples too, all yielded similar results, few resulting in 'None' as output for Y/N.
sample.colval[0:20]

0 N
1 N 2 N
3 N
4 N
5 N
6 N
7 N
8 N
9 N
10 N
11 Y
12 N
13 N
14 N
15 N
16 N
17 N
18 Y
19 N
Name: colval, dtype: object

Please help, Thank you

Tia
  • 521
  • 2
  • 6
  • 18
  • just use some software such as libre calc or excel or whatever and replace all in selection... no reason to overthink – Aleksei Maide May 11 '18 at 06:10
  • Do you have the case matching, you could do `0 if x.lower() == 'no' else 1`? If you can have both `no` and `n` then you can use `in` instead of `==`. – AChampion May 11 '18 at 06:12
  • You should show an example of the data it's being used on (is it Yes/No, yes/no, or Y/N? you use them interchangeably in the question, but it matters, as `==` will only be true for exact matches) – janusvm May 11 '18 at 06:13
  • 1
    `'N' == 'no'` is false, so it will be converted to 1. If you want to match `N`, you have to compare it to `'N'`. If you want to match anything starting with lowercase or uppercase `N`, you can write that—e.g., `if x.lower().startswith('n')`. But whatever you want to do, you have to write _that_; Python can't guess what you actually meant to write. – abarnert May 11 '18 at 06:13
  • @janusvm It's Y/N , not yes/no or y/n. – Tia May 11 '18 at 06:21
  • @Vivia in that case, you need to check for `x == 'N'`, since 'N' and 'no' are two different strings. – janusvm May 11 '18 at 06:24
  • @janusvm Yes, I tried that as well, it's still the same – Tia May 11 '18 at 06:29
  • @Vivia If you want useful answers, you're gonna have to post a more complete and reproducible example (i.e. one we can copy and paste to get the same error as you) – janusvm May 11 '18 at 06:35
  • @janusvm I have added the sample of the data column in the question now. – Tia May 11 '18 at 06:45
  • @Vivia with the information given in your question, I am unable to reproduce your error. Given that `colval` is a string column of 'Y' and 'N', `sample['colval'].apply(lambda x: 0 if x=='N' else 1)` indeed converts them as wanted. It's hard to pin down why you can't, since your example code is very incomplete. Please see https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples and https://stackoverflow.com/help/mcve – janusvm May 11 '18 at 07:02

1 Answers1

0

Found it difficult to repeat given the older version of Python and no access to the original data but your output above suggests whitespace in with the value. Try

sample['colval'] = sample['colval'].apply(lambda x: 0 if x.strip()=='N' else 1)
Mark
  • 86
  • 3