Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

maping value to data in pandas dataframe

note: u can assigne values in each of the common values in the dataframe 

df['new_coloum'] = df['coloum'].map({'value_1':1,'value_2':0})
Comment

Map values in Pandas column using dictionary

Subjects = {"Sheldon" : "Science",
            "Raj" : "Chemistry",
            "Leonard" : "Maths",
            "Howard" : "Astronaut",
            "Amy" : "Science"}
                     
df["Subjects"] = df["first_name"].map(Subjects)
Comment

pandas dataframe map

df.applymap(lambda x: x**2)
Comment

map column dataframe python

df = pd.DataFrame({'col1':pd.date_range('2015-01-02 15:00:07', periods=3),
                   'col2':pd.date_range('2015-05-02 15:00:07', periods=3),
                   'col3':pd.date_range('2015-04-02 15:00:07', periods=3),
                   'col4':pd.date_range('2015-09-02 15:00:07', periods=3),
                   'col5':[5,3,6],
                   'col6':[7,4,3]})

print (df)
                 col1                col2                col3  
0 2015-01-02 15:00:07 2015-05-02 15:00:07 2015-04-02 15:00:07   
1 2015-01-03 15:00:07 2015-05-03 15:00:07 2015-04-03 15:00:07   
2 2015-01-04 15:00:07 2015-05-04 15:00:07 2015-04-04 15:00:07   

                 col4  col5  col6  
0 2015-09-02 15:00:07     5     7  
1 2015-09-03 15:00:07     3     4  
2 2015-09-04 15:00:07     6     3  

list_of_cols_to_change = ['col1','col2','col3','col4']
df[list_of_cols_to_change] = df[list_of_cols_to_change].apply(lambda x: x.dt.date)
print (df)
         col1        col2        col3        col4  col5  col6
0  2015-01-02  2015-05-02  2015-04-02  2015-09-02     5     7
1  2015-01-03  2015-05-03  2015-04-03  2015-09-03     3     4
2  2015-01-04  2015-05-04  2015-04-04  2015-09-04     6     3
Comment

map column dataframe python

df = pd.DataFrame({'col1':pd.date_range('2015-01-02 15:00:07', periods=3),
                   'col2':pd.date_range('2015-05-02 15:00:07', periods=3),
                   'col3':pd.date_range('2015-04-02 15:00:07', periods=3),
                   'col4':pd.date_range('2015-09-02 15:00:07', periods=3),
                   'col5':[5,3,6],
                   'col6':[7,4,3]})

print (df)
                 col1                col2                col3  
0 2015-01-02 15:00:07 2015-05-02 15:00:07 2015-04-02 15:00:07   
1 2015-01-03 15:00:07 2015-05-03 15:00:07 2015-04-03 15:00:07   
2 2015-01-04 15:00:07 2015-05-04 15:00:07 2015-04-04 15:00:07   

                 col4  col5  col6  
0 2015-09-02 15:00:07     5     7  
1 2015-09-03 15:00:07     3     4  
2 2015-09-04 15:00:07     6     3  

list_of_cols_to_change = ['col1','col2','col3','col4']
df[list_of_cols_to_change] = df[list_of_cols_to_change].apply(lambda x: x.dt.date)
print (df)
         col1        col2        col3        col4  col5  col6
0  2015-01-02  2015-05-02  2015-04-02  2015-09-02     5     7
1  2015-01-03  2015-05-03  2015-04-03  2015-09-03     3     4
2  2015-01-04  2015-05-04  2015-04-04  2015-09-04     6     3
Comment

Syntax of pandas map()

# Syntax of Series.map()
Series.map(arg, na_action=None)
Comment

map dataframe

>>> s.map({'cat': 'kitten', 'dog': 'puppy'})
0   kitten
1    puppy
2      NaN
3      NaN
dtype: object
Comment

map column dataframe python

df = pd.DataFrame({'col1':pd.date_range('2015-01-02 15:00:07', periods=3),
                   'col2':pd.date_range('2015-05-02 15:00:07', periods=3),
                   'col3':pd.date_range('2015-04-02 15:00:07', periods=3),
                   'col4':pd.date_range('2015-09-02 15:00:07', periods=3),
                   'col5':[5,3,6],
                   'col6':[7,4,3]})

print (df)
                 col1                col2                col3  
0 2015-01-02 15:00:07 2015-05-02 15:00:07 2015-04-02 15:00:07   
1 2015-01-03 15:00:07 2015-05-03 15:00:07 2015-04-03 15:00:07   
2 2015-01-04 15:00:07 2015-05-04 15:00:07 2015-04-04 15:00:07   

                 col4  col5  col6  
0 2015-09-02 15:00:07     5     7  
1 2015-09-03 15:00:07     3     4  
2 2015-09-04 15:00:07     6     3  

list_of_cols_to_change = ['col1','col2','col3','col4']
df[list_of_cols_to_change] = df[list_of_cols_to_change].apply(lambda x: x.dt.date)
print (df)
         col1        col2        col3        col4  col5  col6
0  2015-01-02  2015-05-02  2015-04-02  2015-09-02     5     7
1  2015-01-03  2015-05-03  2015-04-03  2015-09-03     3     4
2  2015-01-04  2015-05-04  2015-04-04  2015-09-04     6     3
Comment

map column dataframe python

df = pd.DataFrame({'col1':pd.date_range('2015-01-02 15:00:07', periods=3),
                   'col2':pd.date_range('2015-05-02 15:00:07', periods=3),
                   'col3':pd.date_range('2015-04-02 15:00:07', periods=3),
                   'col4':pd.date_range('2015-09-02 15:00:07', periods=3),
                   'col5':[5,3,6],
                   'col6':[7,4,3]})

print (df)
                 col1                col2                col3  
0 2015-01-02 15:00:07 2015-05-02 15:00:07 2015-04-02 15:00:07   
1 2015-01-03 15:00:07 2015-05-03 15:00:07 2015-04-03 15:00:07   
2 2015-01-04 15:00:07 2015-05-04 15:00:07 2015-04-04 15:00:07   

                 col4  col5  col6  
0 2015-09-02 15:00:07     5     7  
1 2015-09-03 15:00:07     3     4  
2 2015-09-04 15:00:07     6     3  

list_of_cols_to_change = ['col1','col2','col3','col4']
df[list_of_cols_to_change] = df[list_of_cols_to_change].apply(lambda x: x.dt.date)
print (df)
         col1        col2        col3        col4  col5  col6
0  2015-01-02  2015-05-02  2015-04-02  2015-09-02     5     7
1  2015-01-03  2015-05-03  2015-04-03  2015-09-03     3     4
2  2015-01-04  2015-05-04  2015-04-04  2015-09-04     6     3
Comment

map column dataframe python

df = pd.DataFrame({'col1':pd.date_range('2015-01-02 15:00:07', periods=3),
                   'col2':pd.date_range('2015-05-02 15:00:07', periods=3),
                   'col3':pd.date_range('2015-04-02 15:00:07', periods=3),
                   'col4':pd.date_range('2015-09-02 15:00:07', periods=3),
                   'col5':[5,3,6],
                   'col6':[7,4,3]})

print (df)
                 col1                col2                col3  
0 2015-01-02 15:00:07 2015-05-02 15:00:07 2015-04-02 15:00:07   
1 2015-01-03 15:00:07 2015-05-03 15:00:07 2015-04-03 15:00:07   
2 2015-01-04 15:00:07 2015-05-04 15:00:07 2015-04-04 15:00:07   

                 col4  col5  col6  
0 2015-09-02 15:00:07     5     7  
1 2015-09-03 15:00:07     3     4  
2 2015-09-04 15:00:07     6     3  

list_of_cols_to_change = ['col1','col2','col3','col4']
df[list_of_cols_to_change] = df[list_of_cols_to_change].apply(lambda x: x.dt.date)
print (df)
         col1        col2        col3        col4  col5  col6
0  2015-01-02  2015-05-02  2015-04-02  2015-09-02     5     7
1  2015-01-03  2015-05-03  2015-04-03  2015-09-03     3     4
2  2015-01-04  2015-05-04  2015-04-04  2015-09-04     6     3
Comment

map column dataframe python

df = pd.DataFrame({'col1':pd.date_range('2015-01-02 15:00:07', periods=3),
                   'col2':pd.date_range('2015-05-02 15:00:07', periods=3),
                   'col3':pd.date_range('2015-04-02 15:00:07', periods=3),
                   'col4':pd.date_range('2015-09-02 15:00:07', periods=3),
                   'col5':[5,3,6],
                   'col6':[7,4,3]})

print (df)
                 col1                col2                col3  
0 2015-01-02 15:00:07 2015-05-02 15:00:07 2015-04-02 15:00:07   
1 2015-01-03 15:00:07 2015-05-03 15:00:07 2015-04-03 15:00:07   
2 2015-01-04 15:00:07 2015-05-04 15:00:07 2015-04-04 15:00:07   

                 col4  col5  col6  
0 2015-09-02 15:00:07     5     7  
1 2015-09-03 15:00:07     3     4  
2 2015-09-04 15:00:07     6     3  

list_of_cols_to_change = ['col1','col2','col3','col4']
df[list_of_cols_to_change] = df[list_of_cols_to_change].apply(lambda x: x.dt.date)
print (df)
         col1        col2        col3        col4  col5  col6
0  2015-01-02  2015-05-02  2015-04-02  2015-09-02     5     7
1  2015-01-03  2015-05-03  2015-04-03  2015-09-03     3     4
2  2015-01-04  2015-05-04  2015-04-04  2015-09-04     6     3
Comment

map column dataframe python

df = pd.DataFrame({'col1':pd.date_range('2015-01-02 15:00:07', periods=3),
                   'col2':pd.date_range('2015-05-02 15:00:07', periods=3),
                   'col3':pd.date_range('2015-04-02 15:00:07', periods=3),
                   'col4':pd.date_range('2015-09-02 15:00:07', periods=3),
                   'col5':[5,3,6],
                   'col6':[7,4,3]})

print (df)
                 col1                col2                col3  
0 2015-01-02 15:00:07 2015-05-02 15:00:07 2015-04-02 15:00:07   
1 2015-01-03 15:00:07 2015-05-03 15:00:07 2015-04-03 15:00:07   
2 2015-01-04 15:00:07 2015-05-04 15:00:07 2015-04-04 15:00:07   

                 col4  col5  col6  
0 2015-09-02 15:00:07     5     7  
1 2015-09-03 15:00:07     3     4  
2 2015-09-04 15:00:07     6     3  

list_of_cols_to_change = ['col1','col2','col3','col4']
df[list_of_cols_to_change] = df[list_of_cols_to_change].apply(lambda x: x.dt.date)
print (df)
         col1        col2        col3        col4  col5  col6
0  2015-01-02  2015-05-02  2015-04-02  2015-09-02     5     7
1  2015-01-03  2015-05-03  2015-04-03  2015-09-03     3     4
2  2015-01-04  2015-05-04  2015-04-04  2015-09-04     6     3
Comment

map column dataframe python

df = pd.DataFrame({'col1':pd.date_range('2015-01-02 15:00:07', periods=3),
                   'col2':pd.date_range('2015-05-02 15:00:07', periods=3),
                   'col3':pd.date_range('2015-04-02 15:00:07', periods=3),
                   'col4':pd.date_range('2015-09-02 15:00:07', periods=3),
                   'col5':[5,3,6],
                   'col6':[7,4,3]})

print (df)
                 col1                col2                col3  
0 2015-01-02 15:00:07 2015-05-02 15:00:07 2015-04-02 15:00:07   
1 2015-01-03 15:00:07 2015-05-03 15:00:07 2015-04-03 15:00:07   
2 2015-01-04 15:00:07 2015-05-04 15:00:07 2015-04-04 15:00:07   

                 col4  col5  col6  
0 2015-09-02 15:00:07     5     7  
1 2015-09-03 15:00:07     3     4  
2 2015-09-04 15:00:07     6     3  

list_of_cols_to_change = ['col1','col2','col3','col4']
df[list_of_cols_to_change] = df[list_of_cols_to_change].apply(lambda x: x.dt.date)
print (df)
         col1        col2        col3        col4  col5  col6
0  2015-01-02  2015-05-02  2015-04-02  2015-09-02     5     7
1  2015-01-03  2015-05-03  2015-04-03  2015-09-03     3     4
2  2015-01-04  2015-05-04  2015-04-04  2015-09-04     6     3
Comment

map column dataframe python

df = pd.DataFrame({'col1':pd.date_range('2015-01-02 15:00:07', periods=3),
                   'col2':pd.date_range('2015-05-02 15:00:07', periods=3),
                   'col3':pd.date_range('2015-04-02 15:00:07', periods=3),
                   'col4':pd.date_range('2015-09-02 15:00:07', periods=3),
                   'col5':[5,3,6],
                   'col6':[7,4,3]})

print (df)
                 col1                col2                col3  
0 2015-01-02 15:00:07 2015-05-02 15:00:07 2015-04-02 15:00:07   
1 2015-01-03 15:00:07 2015-05-03 15:00:07 2015-04-03 15:00:07   
2 2015-01-04 15:00:07 2015-05-04 15:00:07 2015-04-04 15:00:07   

                 col4  col5  col6  
0 2015-09-02 15:00:07     5     7  
1 2015-09-03 15:00:07     3     4  
2 2015-09-04 15:00:07     6     3  

list_of_cols_to_change = ['col1','col2','col3','col4']
df[list_of_cols_to_change] = df[list_of_cols_to_change].apply(lambda x: x.dt.date)
print (df)
         col1        col2        col3        col4  col5  col6
0  2015-01-02  2015-05-02  2015-04-02  2015-09-02     5     7
1  2015-01-03  2015-05-03  2015-04-03  2015-09-03     3     4
2  2015-01-04  2015-05-04  2015-04-04  2015-09-04     6     3
Comment

map column dataframe python

df = pd.DataFrame({'col1':pd.date_range('2015-01-02 15:00:07', periods=3),
                   'col2':pd.date_range('2015-05-02 15:00:07', periods=3),
                   'col3':pd.date_range('2015-04-02 15:00:07', periods=3),
                   'col4':pd.date_range('2015-09-02 15:00:07', periods=3),
                   'col5':[5,3,6],
                   'col6':[7,4,3]})

print (df)
                 col1                col2                col3  
0 2015-01-02 15:00:07 2015-05-02 15:00:07 2015-04-02 15:00:07   
1 2015-01-03 15:00:07 2015-05-03 15:00:07 2015-04-03 15:00:07   
2 2015-01-04 15:00:07 2015-05-04 15:00:07 2015-04-04 15:00:07   

                 col4  col5  col6  
0 2015-09-02 15:00:07     5     7  
1 2015-09-03 15:00:07     3     4  
2 2015-09-04 15:00:07     6     3  

list_of_cols_to_change = ['col1','col2','col3','col4']
df[list_of_cols_to_change] = df[list_of_cols_to_change].apply(lambda x: x.dt.date)
print (df)
         col1        col2        col3        col4  col5  col6
0  2015-01-02  2015-05-02  2015-04-02  2015-09-02     5     7
1  2015-01-03  2015-05-03  2015-04-03  2015-09-03     3     4
2  2015-01-04  2015-05-04  2015-04-04  2015-09-04     6     3
Comment

map column dataframe python

df = pd.DataFrame({'col1':pd.date_range('2015-01-02 15:00:07', periods=3),
                   'col2':pd.date_range('2015-05-02 15:00:07', periods=3),
                   'col3':pd.date_range('2015-04-02 15:00:07', periods=3),
                   'col4':pd.date_range('2015-09-02 15:00:07', periods=3),
                   'col5':[5,3,6],
                   'col6':[7,4,3]})

print (df)
                 col1                col2                col3  
0 2015-01-02 15:00:07 2015-05-02 15:00:07 2015-04-02 15:00:07   
1 2015-01-03 15:00:07 2015-05-03 15:00:07 2015-04-03 15:00:07   
2 2015-01-04 15:00:07 2015-05-04 15:00:07 2015-04-04 15:00:07   

                 col4  col5  col6  
0 2015-09-02 15:00:07     5     7  
1 2015-09-03 15:00:07     3     4  
2 2015-09-04 15:00:07     6     3  

list_of_cols_to_change = ['col1','col2','col3','col4']
df[list_of_cols_to_change] = df[list_of_cols_to_change].apply(lambda x: x.dt.date)
print (df)
         col1        col2        col3        col4  col5  col6
0  2015-01-02  2015-05-02  2015-04-02  2015-09-02     5     7
1  2015-01-03  2015-05-03  2015-04-03  2015-09-03     3     4
2  2015-01-04  2015-05-04  2015-04-04  2015-09-04     6     3
Comment

map column dataframe python

df = pd.DataFrame({'col1':pd.date_range('2015-01-02 15:00:07', periods=3),
                   'col2':pd.date_range('2015-05-02 15:00:07', periods=3),
                   'col3':pd.date_range('2015-04-02 15:00:07', periods=3),
                   'col4':pd.date_range('2015-09-02 15:00:07', periods=3),
                   'col5':[5,3,6],
                   'col6':[7,4,3]})

print (df)
                 col1                col2                col3  
0 2015-01-02 15:00:07 2015-05-02 15:00:07 2015-04-02 15:00:07   
1 2015-01-03 15:00:07 2015-05-03 15:00:07 2015-04-03 15:00:07   
2 2015-01-04 15:00:07 2015-05-04 15:00:07 2015-04-04 15:00:07   

                 col4  col5  col6  
0 2015-09-02 15:00:07     5     7  
1 2015-09-03 15:00:07     3     4  
2 2015-09-04 15:00:07     6     3  

list_of_cols_to_change = ['col1','col2','col3','col4']
df[list_of_cols_to_change] = df[list_of_cols_to_change].apply(lambda x: x.dt.date)
print (df)
         col1        col2        col3        col4  col5  col6
0  2015-01-02  2015-05-02  2015-04-02  2015-09-02     5     7
1  2015-01-03  2015-05-03  2015-04-03  2015-09-03     3     4
2  2015-01-04  2015-05-04  2015-04-04  2015-09-04     6     3
Comment

map column dataframe python

df = pd.DataFrame({'col1':pd.date_range('2015-01-02 15:00:07', periods=3),
                   'col2':pd.date_range('2015-05-02 15:00:07', periods=3),
                   'col3':pd.date_range('2015-04-02 15:00:07', periods=3),
                   'col4':pd.date_range('2015-09-02 15:00:07', periods=3),
                   'col5':[5,3,6],
                   'col6':[7,4,3]})

print (df)
                 col1                col2                col3  
0 2015-01-02 15:00:07 2015-05-02 15:00:07 2015-04-02 15:00:07   
1 2015-01-03 15:00:07 2015-05-03 15:00:07 2015-04-03 15:00:07   
2 2015-01-04 15:00:07 2015-05-04 15:00:07 2015-04-04 15:00:07   

                 col4  col5  col6  
0 2015-09-02 15:00:07     5     7  
1 2015-09-03 15:00:07     3     4  
2 2015-09-04 15:00:07     6     3  

list_of_cols_to_change = ['col1','col2','col3','col4']
df[list_of_cols_to_change] = df[list_of_cols_to_change].apply(lambda x: x.dt.date)
print (df)
         col1        col2        col3        col4  col5  col6
0  2015-01-02  2015-05-02  2015-04-02  2015-09-02     5     7
1  2015-01-03  2015-05-03  2015-04-03  2015-09-03     3     4
2  2015-01-04  2015-05-04  2015-04-04  2015-09-04     6     3
Comment

map column dataframe python

df = pd.DataFrame({'col1':pd.date_range('2015-01-02 15:00:07', periods=3),
                   'col2':pd.date_range('2015-05-02 15:00:07', periods=3),
                   'col3':pd.date_range('2015-04-02 15:00:07', periods=3),
                   'col4':pd.date_range('2015-09-02 15:00:07', periods=3),
                   'col5':[5,3,6],
                   'col6':[7,4,3]})

print (df)
                 col1                col2                col3  
0 2015-01-02 15:00:07 2015-05-02 15:00:07 2015-04-02 15:00:07   
1 2015-01-03 15:00:07 2015-05-03 15:00:07 2015-04-03 15:00:07   
2 2015-01-04 15:00:07 2015-05-04 15:00:07 2015-04-04 15:00:07   

                 col4  col5  col6  
0 2015-09-02 15:00:07     5     7  
1 2015-09-03 15:00:07     3     4  
2 2015-09-04 15:00:07     6     3  

list_of_cols_to_change = ['col1','col2','col3','col4']
df[list_of_cols_to_change] = df[list_of_cols_to_change].apply(lambda x: x.dt.date)
print (df)
         col1        col2        col3        col4  col5  col6
0  2015-01-02  2015-05-02  2015-04-02  2015-09-02     5     7
1  2015-01-03  2015-05-03  2015-04-03  2015-09-03     3     4
2  2015-01-04  2015-05-04  2015-04-04  2015-09-04     6     3
Comment

map column dataframe python

df = pd.DataFrame({'col1':pd.date_range('2015-01-02 15:00:07', periods=3),
                   'col2':pd.date_range('2015-05-02 15:00:07', periods=3),
                   'col3':pd.date_range('2015-04-02 15:00:07', periods=3),
                   'col4':pd.date_range('2015-09-02 15:00:07', periods=3),
                   'col5':[5,3,6],
                   'col6':[7,4,3]})

print (df)
                 col1                col2                col3  
0 2015-01-02 15:00:07 2015-05-02 15:00:07 2015-04-02 15:00:07   
1 2015-01-03 15:00:07 2015-05-03 15:00:07 2015-04-03 15:00:07   
2 2015-01-04 15:00:07 2015-05-04 15:00:07 2015-04-04 15:00:07   

                 col4  col5  col6  
0 2015-09-02 15:00:07     5     7  
1 2015-09-03 15:00:07     3     4  
2 2015-09-04 15:00:07     6     3  

list_of_cols_to_change = ['col1','col2','col3','col4']
df[list_of_cols_to_change] = df[list_of_cols_to_change].apply(lambda x: x.dt.date)
print (df)
         col1        col2        col3        col4  col5  col6
0  2015-01-02  2015-05-02  2015-04-02  2015-09-02     5     7
1  2015-01-03  2015-05-03  2015-04-03  2015-09-03     3     4
2  2015-01-04  2015-05-04  2015-04-04  2015-09-04     6     3
Comment

map column dataframe python

df = pd.DataFrame({'col1':pd.date_range('2015-01-02 15:00:07', periods=3),
                   'col2':pd.date_range('2015-05-02 15:00:07', periods=3),
                   'col3':pd.date_range('2015-04-02 15:00:07', periods=3),
                   'col4':pd.date_range('2015-09-02 15:00:07', periods=3),
                   'col5':[5,3,6],
                   'col6':[7,4,3]})

print (df)
                 col1                col2                col3  
0 2015-01-02 15:00:07 2015-05-02 15:00:07 2015-04-02 15:00:07   
1 2015-01-03 15:00:07 2015-05-03 15:00:07 2015-04-03 15:00:07   
2 2015-01-04 15:00:07 2015-05-04 15:00:07 2015-04-04 15:00:07   

                 col4  col5  col6  
0 2015-09-02 15:00:07     5     7  
1 2015-09-03 15:00:07     3     4  
2 2015-09-04 15:00:07     6     3  

list_of_cols_to_change = ['col1','col2','col3','col4']
df[list_of_cols_to_change] = df[list_of_cols_to_change].apply(lambda x: x.dt.date)
print (df)
         col1        col2        col3        col4  col5  col6
0  2015-01-02  2015-05-02  2015-04-02  2015-09-02     5     7
1  2015-01-03  2015-05-03  2015-04-03  2015-09-03     3     4
2  2015-01-04  2015-05-04  2015-04-04  2015-09-04     6     3
Comment

map column dataframe python

df = pd.DataFrame({'col1':pd.date_range('2015-01-02 15:00:07', periods=3),
                   'col2':pd.date_range('2015-05-02 15:00:07', periods=3),
                   'col3':pd.date_range('2015-04-02 15:00:07', periods=3),
                   'col4':pd.date_range('2015-09-02 15:00:07', periods=3),
                   'col5':[5,3,6],
                   'col6':[7,4,3]})

print (df)
                 col1                col2                col3  
0 2015-01-02 15:00:07 2015-05-02 15:00:07 2015-04-02 15:00:07   
1 2015-01-03 15:00:07 2015-05-03 15:00:07 2015-04-03 15:00:07   
2 2015-01-04 15:00:07 2015-05-04 15:00:07 2015-04-04 15:00:07   

                 col4  col5  col6  
0 2015-09-02 15:00:07     5     7  
1 2015-09-03 15:00:07     3     4  
2 2015-09-04 15:00:07     6     3  

list_of_cols_to_change = ['col1','col2','col3','col4']
df[list_of_cols_to_change] = df[list_of_cols_to_change].apply(lambda x: x.dt.date)
print (df)
         col1        col2        col3        col4  col5  col6
0  2015-01-02  2015-05-02  2015-04-02  2015-09-02     5     7
1  2015-01-03  2015-05-03  2015-04-03  2015-09-03     3     4
2  2015-01-04  2015-05-04  2015-04-04  2015-09-04     6     3
Comment

map column dataframe python

df = pd.DataFrame({'col1':pd.date_range('2015-01-02 15:00:07', periods=3),
                   'col2':pd.date_range('2015-05-02 15:00:07', periods=3),
                   'col3':pd.date_range('2015-04-02 15:00:07', periods=3),
                   'col4':pd.date_range('2015-09-02 15:00:07', periods=3),
                   'col5':[5,3,6],
                   'col6':[7,4,3]})

print (df)
                 col1                col2                col3  
0 2015-01-02 15:00:07 2015-05-02 15:00:07 2015-04-02 15:00:07   
1 2015-01-03 15:00:07 2015-05-03 15:00:07 2015-04-03 15:00:07   
2 2015-01-04 15:00:07 2015-05-04 15:00:07 2015-04-04 15:00:07   

                 col4  col5  col6  
0 2015-09-02 15:00:07     5     7  
1 2015-09-03 15:00:07     3     4  
2 2015-09-04 15:00:07     6     3  

list_of_cols_to_change = ['col1','col2','col3','col4']
df[list_of_cols_to_change] = df[list_of_cols_to_change].apply(lambda x: x.dt.date)
print (df)
         col1        col2        col3        col4  col5  col6
0  2015-01-02  2015-05-02  2015-04-02  2015-09-02     5     7
1  2015-01-03  2015-05-03  2015-04-03  2015-09-03     3     4
2  2015-01-04  2015-05-04  2015-04-04  2015-09-04     6     3
Comment

PREVIOUS NEXT
Code Example
Python :: how to unpack the whole list without index them individually python 
Python :: Mapping using dictionary 
Python :: Dataframe with defined shape filled with 0 
Python :: Donut chart graphing funciton 
Python :: pyqt set widget size 
Python :: extract arabic text from image python 
Python :: python google translator 
Python :: new library in python3 
Python :: python tkinter button multiple commands 
Python :: python chatbot speech recognition 
Python :: write yaml file without deleting content 
Python :: tkinter radiobutton "bind_all" 
Python :: how to add previous and next in tkinter in python 
Python :: add values to add value in a matplotlib image 
Python :: python requests json backslash 
Python :: python lxml get parent 
Python :: expionenttiation python 
Python :: print less than specific number in one row python 
Python :: python print to string 
Python :: add fully connected layers at encoder of autoencoder 
Python :: the dropping of sediment by water wind and ice or gravity is known as 
Python :: Save this RDD as a SequenceFile of serialized objects 
Python :: Applies the f function to all Row 
Python :: how to upgrade python from 2.7 to 2.9 on ubuntu 14.04 
Python :: sns.distplot fit 
Python :: 2sf python 
Python :: gtk entry focus python 
Python :: % python nootation 
Python :: c++ code to python code converter online 
Python :: number of features classification model jupyter notebook 
ADD CONTENT
Topic
Content
Source link
Name
3+1 =