Home > Blockchain >  Reading data and converting information once
Reading data and converting information once

Time:08-12

I am importing a huge data from a CSV file (but my doubt would apply to other kinds of pd.read_X)

When I read the file, there is a integer column called comunication_channel using a list numbers representing several types of communication.

I would like to replace such column (or convert values) to a String that represents that channel (email, push, etc).

That could bem by somenthing like what would be a SQL Join or a if/else code, for example.

Is there a simple way for that?

CodePudding user response:

read_csv takes a converters argument, with which you can transform the input value per individual column:

import pandas as pd
from collections import defaultdict 

# create default dict to host the integer->name mappings
channel_map = defaultdict(lambda: 'UNKNOWN', {1: 'Channel1', 2: 'Channel2', 3: 'Channel3'})

# specify a conversion routine for the column when importing
pd.read_csv(r"path/to/file.csv", converters={'column_name': lambda v: channel_map[v]})
  • Related