简体   繁体   中英

Most efficient way of mapping column in pandas DataFrame

I was wondering if map method was the best option when a simple mapping was necessary in a column, since using map or apply is usually a bad idea .

I compared the following functions for the simple case below. Please share if you have better alternatives.

# Case - Map the random number to its string
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randint(1,7,size=(5000,1)), columns=['A'])
dikt = {1:'1',2:'2',3:'3',4:'4',5:'5',6:'6'}

First function - using map method:

def f1():
    df1 = df.copy()
    df1['B'] = df['A'].map(dikt)
    return df1

Results: 在此处输入图像描述

Second function - using to_list method in column:

def f2():
    df2 = df.copy()
    column_list = df2['A'].tolist()
    df2['B'] = [dikt[i] for i in column_list]
    return df2

Results: 在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM