Mapping external values to dataframe values in Pandas
Mapping external value to a dataframe means using different sets of values to add in that dataframe by keeping the keys of external dictionary as same as the one column of that dataframe.
To add external values in dataframe, we use dictionary which has keys and values which we want to add in the dataframe. By adding external values in the dataframe one column will be added to the current dataframe. We can also map or combine one dataframe to other dataframe with the help of pandas.
Method #1: Using mapping function
By using this mapping function we can add one more column to an existing dataframe. Just keep in mind that no key values will be repeated it will make the data inconsistent.
First_name Last_name Age City Qualification 0 Ram Kumar 42 Mumbai B.Com 1 Mohan Sharma 52 Noida IAS 2 Tina Ali 36 Pune LLB 3 Jeetu Gandhi 21 Delhi B.Tech 4 Meera Kumari 23 Bihar MBBS
Method #2: Using
In this method, we can add or replace some of the value of the dataframe by some defined external values.
First_name Last_name Age City 0 Ram Kumar 42 Mumbai 1 Mohan Sharma 52 Noida 2 Tina Ali 36 Pune 3 Jeetu Gandhi 21 Delhi 4 Meera Kumari 23 Bihar First_name Last_name Age City 0 Shyam Kumar 42 Mumbai 1 Mohan Sharma 52 Noida 2 Riya Ali 36 Pune 3 Jitender Gandhi 21 Delhi 4 Meera Kumari 23 Bihar
Method #3: Using
In this method we can update the dataframe values by using index values we can change the value of columns by external data.
First_name Last_name Age City 0 Shyam Kumar 42 Mumbai 1 Mohan Sharma 52 Noida 2 Riya Ali 36 Pune 3 Jitender Gandhi 21 Delhi 4 Meera Kumari 23 Bihar
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course