Assign numeric value to text in python

I want to assign a specific integer value to a text type input in python for further indexing.

Example- I input 'sap' as text and i want it to be assigned as 1.

P.s I'm new to coding so spare me for wrong technical terms

asked Jun 8, 2020 at 17:54

4

Two ways:

1: You can assign a value to a string with a dictionary, like so:

text = input["Input your text: "]
value = int[input[f"Input the value of {text}:"]]
d = {text: value}

Output:

Input your text: hi
Input the value of hi: 6
# {'hi': 6}

2: Or, if you want to keep the text as a variable:

text = input["Input your text: "]
value = int[input[f"Input the value of {text}:"]]
locals[][text] = value

Output:

Input your text: hi
Input the value of hi: 6
# hi = 6

answered Jun 8, 2020 at 19:01

Ann ZenAnn Zen

25.7k7 gold badges31 silver badges52 bronze badges

You could do this via a dict and the below function in example

my_index = {}

def add_element[index,el]:
    my_index[index] = el

add_element[0,"sap"]
add_element[1,"spam"]

print[my_index]

OUT: {0: 'sap', 1: 'spam'}

answered Jun 8, 2020 at 17:57

Nico MüllerNico Müller

1,6471 gold badge17 silver badges31 bronze badges

0

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Sometimes we need to convert string values in a pandas dataframe to a unique integer so that the algorithms can perform better. So we assign unique numeric value to a string value in Pandas DataFrame.

    Note: Before executing create an example.csv file containing some names and gender

    Say we have a table containing names and gender column. In gender column, there are two categories male and female and suppose we want to assign 1 to male and 2 to female.

    Examples:

    Input : 
    ---------------------
        |  Name  |  Gender
    ---------------------
     0    Ram        Male
     1    Seeta      Female
     2    Kartik     Male
     3    Niti       Female
     4    Naitik     Male 
    
    Output :
        |  Name  |  Gender
    ---------------------
     0    Ram        1
     1    Seeta      2
     2    Kartik     1
     3    Niti       2
     4    Naitik     1 
    

    Recommended: Please try your approach on {IDE} first, before moving on to the solution.

    Method 1:

    To create a dictionary containing two 
    elements with following key-value pair:
    Key       Value
    male      1
    female    2
    

    Then iterate using for loop through Gender column of DataFrame and replace the values wherever the keys are found.

    import pandas as pd

    file_handler = open["example.csv", "r"]

    data = pd.read_csv[file_handler, sep = ","]

    file_handler.close[]

    gender = {'male': 1,'female': 2}

    data.Gender = [gender[item] for item in data.Gender]

    print[data]

    Output :

        |  Name  |  Gender
    ---------------------
     0    Ram        1
     1    Seeta      2
     2    Kartik     1
     3    Niti       2
     4    Naitik     1 
    

    Method 2:
    Method 2 is also similar but requires no dictionary file and takes fewer lines of code. In this, we internally iterate through Gender column of DataFrame and change the values if the condition matches.

    import pandas as pd

    file_handler = open["example.csv", "r"]

    data = pd.read_csv[file_handler, sep = ","]

    file_handler.close[]

    data.Gender[data.Gender == 'male'] = 1

    data.Gender[data.Gender == 'female'] = 2

    print[data]

    Output :

        |  Name  |  Gender
    ---------------------
     0    Ram        1
     1    Seeta      2
     2    Kartik     1
     3    Niti       2
     4    Naitik     1 
    

    Applications

    1. This technique can be applied in Data Science. Suppose if we are working on a dataset that contains gender as ‘male’ and ‘female’ then we can assign numbers like ‘0’ and ‘1’ respectively so that our algorithms can work on the data.
    2. This technique can also be applied to replace some particular values in a datasets with new values.

    References

    • //pandas.pydata.org
    • //pandas.pydata.org/pandas-docs/stable/tutorials.html

    How do you assign a numerical value to a string in Python?

    Using the str[] Function The str[] function can be used to change any numeric type to a string.

    How to assign a word a numerical value in Python?

    Another way to assign values to words is to construct key-value pairs. In a key-value pair, each key, which can be anything including words, has an associated value. So, for example, the key "Father" could have a value of "Bob." Or, more relevantly, a key of "Hits" [or any word] could have a value of 55.

    How do you assign numbers to letters in Python?

    To set the value of a variable, type the character[s] you want followed by "=" and then the desired value. Ex: x = 5. The variable "x" will now have the value of "5" until specified otherwise. Non mathematical values, such as a string of characters, are also allowed in Python.

    What is a numeric value in Python?

    Numeric values can be of integer and floating-point types. The TestComplete scripting engine does not distinguish floating-point and integer data types, so a variable can have a value of both types. An integer value can accept zero, positive and negative numbers within the range ±1.7976931348623157x10^308.

    Chủ Đề