How to map a column's value to a new column

Viewed 62

I am new with python, and have a dataset as below:

package_name,name,starttime,duration   
com.instagram.android,Instagram,2020-10-19 21:18:57.527,0.116
com.instagram.android,Instagram,2020-10-19 21:26:42.613,3.945
com.facebook.katana,Facebook,2020-10-19 21:26:45.050,1.277
,sudoku,2020-10-30 20:20:45.050,25.277  

I am trying to add column that contains the category of the application. For example: Facebook and Instagram = 'communication', sudoku = 'games', ...

How can I accomplish this on a very large dataset?

1 Answers

Essentially, you want to map a column's value to a lookup table. For this, the pandas.Series.map() function is very useful.

The official docs can be found here.

Steps:

  • Create a lookup table (or dict to be specific)
  • Read your data into a pandas.DataFrame
  • Call the map function on the key column to be mapped
  • Assign the output to a new column

Example code:

import pandas as pd

# Create a lookup table (dict).
lookup = {'instagram': 'communication',
          'facebook': 'communication',
          'sudoku': 'games'}

# Use DataFrame in OP's question.
df = pd.read_clipboard(sep=',')
# Alternatively, the dataset can be read data from a CSV file.
# df = pd.read_csv('/path/to/file.csv', sep=',')
  
# Map the values from `name` column (lower cased) to the category.
df['category'] = df['name'].str.lower().map(lookup)

Output:

            package_name       name                starttime  duration     \
0  com.instagram.android  Instagram  2020-10-19 21:18:57.527        0.116   
1  com.instagram.android  Instagram  2020-10-19 21:26:42.613        3.945   
2    com.facebook.katana   Facebook  2020-10-19 21:26:45.050        1.277   
3                    NaN     sudoku  2020-10-30 20:20:45.050       25.277   

        category  
0  communication  
1  communication  
2  communication  
3          games  
Related