2

I have csv file, with column names of: PH, K, Zn, S, Fe ...so on. For each column there hare 3 different types of output, for example:

PH       S       Zn     Fe         Cu   Mn
Acidic   Low    Low     Medium  High    Medium
Alkaline High   Medium  Medium  High    High
Acidic  Medium  Low     Medium  High    High
Neutral High    Low     Medium  High    High
Acidic   Low    Low     Medium  High    High
Acidic   Low    Low     Medium  High    High
Acidic  Medium  Medium  Medium  High    High

I want to give values of

Acidic = 0, Neutral = 1, Alkaline = 2
Low = 0, Medium = 1, High = 2

How to write a code which is automatic convert Acidic=0 , Neutral=1 , Alkaline=2?

6
  • 1
    0 1 2 is not binary ... 0, 1 is binary Commented Feb 20, 2017 at 6:45
  • @CedricDruck, Oh, Okay, How to convert Low, Medium, High in terms of 0, 1, 2 ? is it possible? Commented Feb 20, 2017 at 6:46
  • 1
    intent or file format is not clear, please elaborate Commented Feb 20, 2017 at 6:46
  • 2
    I'd use dicts .. something like ph_dict = {'Acidic': 1, 'Neutral': 2, 'Alkaline': 3} Commented Feb 20, 2017 at 6:47
  • 1
    If you are working on some machine learning tasks and are using a library for that, scikit-learn and pandas have some inbuild functions for doing that. Commented Feb 20, 2017 at 7:07

2 Answers 2

3

Ok so something like

dicts = {
   'PH' : {'Acidic': 0, 'Alkaline': 1, 'Neutral': 2},
   'S': {'Low': 0, 'High': 1, 'Medium': 2},
   # etc
} 
with open(your_file) as file:
    table = []
    reader = csv.DictReader(file)
    for row in reader:
        new_row = {key: dicts[key][value] for (key, value) in row.items()}
        table.append(new_row)            
Sign up to request clarification or add additional context in comments.

6 Comments

Yes, that's actual Python code .. what's the question?
your_file means my file name is samplefile.csv so i have use with open (samplefile.csv) as file : ?
you have to use some sort of path to your file, yes. See docs.python.org/2/tutorial/…
and reader = csv.DictReader(file) so, here I have use file or something my path ?
no, file is the result of open(your_file), you just have to adapt the "your_file" variable
|
1

Python provides the Enum class. Why you want to use Enums is covered here. In your case they would look something like:

Code:

from enum import Enum
PH = Enum('PH', 'Acidic Neutral Alkaline')
Concentration = Enum('Concentration', 'Low Medium High')

Demo Code:

print(PH['Acidic'].value)
print(Concentration['Medium'].value)

Produces:

1
2

Demo Code2:

for i in range(1, 4):
    ph = PH(i)
    concentration = Concentration(i)
    print(ph, ph.name, ph.value)
    print(concentration, concentration.name, concentration.value)

Produces:

PH.Acidic Acidic 1
Concentration.Low Low 1
PH.Neutral Neutral 2
Concentration.Medium Medium 2
PH.Alkaline Alkaline 3
Concentration.High High 3

2 Comments

Can I use 1,2,3, numbers output for my next analysis using enum?
Yes, the value is a number, see examples.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.