-2

My question is about replacing a numerical value to string in csv file using python.The purpose is to calculate CDF(Cumulative Distribution Function).

Name of the data set is 'hsb', the class label is 'status' which has 304 rows of numerical data 1s and 2s. I want to replace 1 with 'positive' and 2 with 'negative'.

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
Eric
  • 31
  • 5
  • 2
    You write code that reads in the data, transforms it as needed and writes it back out as file. SO is about fixing _your_ Code - not wrinting your code. Please go over [how to ask](https://stackoverflow.com/help/how-to-ask) and [on-topic](https://stackoverflow.com/help/on-topic) again and if you have questions provide your code as [minimal verifyable complete example](https://stackoverflow.com/help/mcve). – Patrick Artner Jun 04 '18 at 18:43

1 Answers1

1

Consider below example which uses .map() map the values from a dict.

 df = pd.DataFrame({
    'col':[1,1,2,2,2,1]
})

Output:

   col
0   1
1   1
2   2
3   2
4   2
5   1

Now,

df['col'] = df['col'].map({1:'positive', 2:'negative'})

Output:

       col
0   positive
1   positive
2   negative
3   negative
4   negative
5   positive
harvpan
  • 8,571
  • 2
  • 18
  • 36