<jupyter_start><jupyter_text>**map** apply a function only to a series(column in a datafram)**apply** can apply a function to multiple columns in a dafaframe, or a series.**applymap** can apply a function element-wise for a dataframe. **applymap** is only a function for dataframe**apply** works on both series and dataframe**map** is only a method for series<jupyter_code>import pandas as pd
import numpy as np
df = pd.DataFrame({'Customer': 'Mr.Burns <NAME> Milhouse <NAME>'.split(),
 'Costs_1': np.arange(6) *2, 'Costs_2': np.arange(6) * 4, 'Costs_3': np.arange(6) * 6})
df<jupyter_output><empty_output><jupyter_text>**apply** a function to a dataframe's rows (vertically)<jupyter_code>def diff_calc(x):
    diff = x.max() - x.min()
    return diff

df[['Costs_1', 'Costs_2', 'Costs_3']].apply(diff_calc)<jupyter_output><empty_output><jupyter_text>**apply** a function to a dataframe's columns (horizontally)<jupyter_code>df[['Costs_1', 'Costs_2', 'Costs_3']].apply(diff_calc, axis=1)<jupyter_output><empty_output><jupyter_text>**apply** works on a series too.<jupyter_code>df['Costs_1'].apply(lambda x: x+1)<jupyter_output><empty_output><jupyter_text>**applymap** a function to all of elements of a dataframe.<jupyter_code>def simpsons_string(x):
    result = "S_" + str(x)
    return result
df.applymap(simpsons_string)<jupyter_output><empty_output><jupyter_text>**applymap** is only a function for dataframe<jupyter_code>df['Costs_1'].applymap(simpsons_string)<jupyter_output><empty_output><jupyter_text>**map** a function only to a single column<jupyter_code>df['Customer'].map(simpsons_string)<jupyter_output><empty_output><jupyter_text>**map** is not a method for dataframe<jupyter_code>df.map(simpsons_string)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14636935391652373, 'fraction_numerical': 0.010863350485991996, 'mean_word_length': 8.308510638297872, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '383028', 'license': 'MIT', 'n_tokens_mistral': 660, 'n_tokens_neox': 643, 'n_words': 142, 'path': 'map, apply, applymap.ipynb', 'repo_name': 'zhensongren/learn-python', 'size': '1757'}