0

I have find the unique value of "NAME" column and assign the numeric value to each name in the csv file ( as given in the dictionary) . I am able to finish that with the below code. can somebody help to achieve the same using lambda function. How can I achieve the same using lambda function.

import pandas as pd
import sys

def dictionaryWithNormalFunction():
    file_loc1= 'filepath'
    # load the data with pd.read_csv
    record = pd.read_csv(file_loc1)
    nameDic ={}
    nameDic={'Rosy':0,'Pinky':1,'Johncy':2,'Mary':3}
    for name,value in nameDic.items():       
        record.loc[record['NAME'].astype(str).str.lower() == name.lower(), "NAME"] = value
    record.to_csv(file_loc1)
dictionaryWithNormalFunction()

The csv data will be as below,

enter image description here

I want to replace the name with student id from the dictionaries,

nameDic={'Rosy':0,'Pinky':1,'Johncy':2,'Mary':3}# this dictionaries data would be in the same case as in csv file. student id and name mapping will be manually added in the dictionary

Replace the name with student id in the csv file using lambda function. how to achieve this using lambda function?

The csv file content should be like below

enter image description here

Rosy
  • 77
  • 1
  • 2
  • 9
  • I don't know why you want to do it with a lambda, but you can achieve the same a bit easier by `record.NAME = record.NAME.str.lower().replace(nameDic)` – Stef Jan 02 '21 at 19:25
  • I am assigning the numeric value for each name from the dictionary to csv file. And This has to be achieved using lambda function. but I am not sure how to perform that. so I proceeded with normal function. – Rosy Jan 03 '21 at 07:05
  • Hi Rosy, as @Stef suggested, you can achieve what you want with just one line of code (or you can also use lambda fucntion), but you need to provide proper information for us to work with. 1) edit your post with some examples and 2) how to get nameDic? do you create it or is it given with the problem? (this is important: need to know whather keys of the dict are in lower case or mixed case) – manju-dev Jan 07 '21 at 10:29
  • check this link to know how to ask good pandas question: https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples – manju-dev Jan 07 '21 at 10:31
  • I have edited my question with example, can you please help me to achieve the same .@Stef @manju-dev – Rosy Jan 08 '21 at 06:12

1 Answers1

0

Here is two ways to achieve what you describe:

# create data
records = pd.DataFrame({'Name': ['Rosy', 'Pinky', 'Johncy', 'Mary'],
                        'Department': ['CSE', 'IT', 'EEE', 'CSE'],
                        'Address': ['DEL', 'MUM', 'CHN', 'HYD']})
nameDic = {'Rosy':0, 'Pinky':1, 'Johncy':2, 'Mary':3}

# using lambda function
records['Name'] = records['Name'].apply(lambda x: nameDic.get(x, 0))

OR

# using built in replace function
records['Name'] = records['Name'].replace(nameDic)
manju-dev
  • 434
  • 2
  • 9
  • 1) is the nameDict created by you (using nameList) or is it given with the problem? - Please ignore the nameList . Consider the below are the excel data. Name Department Address Rosy CSE CHN Pinky IT HYD Johncy CSE MUM Mary EEE DEL I am trying to replace name with Employee id ( ex, Name : Rosy should get updated with empid 0). – Rosy Jan 06 '21 at 17:34
  • Please check and help me on doing the same using lambda function – Rosy Jan 06 '21 at 17:38