## Hackerss.com is a community of amazing hackers

Hackerss is a community for developers, data scientitst, ethical hackers, hardware enthusiasts or any person that want to learn / share their knowledge of any aspect of digital technology.

Posted on

# Python Iterate dataframe fast way

``````
import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=list('ABCD'))

#%%
"""
iterrows()
"""

for index, row in df.iterrows():
print(row['A'], row['B'])

#%%
"""
itertuples()
"""

for row in df.itertuples():
print(row.A, row.B)

#%%
"""
iteritems()
"""

for col, value in df.iteritems():
print(col, value)

#%%
"""
apply()
"""

df.apply(np.sum)

#%%
"""
applymap()
"""

df.applymap(lambda x: x*100)

#%%
"""
map()
"""

df['A'].map(lambda x: x*100)

#%%
"""
apply()
"""

df.apply(np.sum, axis=1)

#%%
"""
apply()
"""

df.apply(lambda x: x.max() - x.min())

#%%
"""
apply()
"""

df.apply(lambda x: x.max() - x.min(), axis=1)

#%%
"""
apply()
"""

df.apply(lambda x: pd.Series([x.min(), x.max()], index=['min', 'max']))

#%%
"""
apply()
"""

df.apply(lambda x: pd.Series([x.min(), x.max()], index=['min', 'max']), axis=1)

#%%
"""
apply()
"""

df.apply(lambda x: pd.Series([x.min(), x.max()], index=['min', 'max']), axis=1).stack()

#%%
"""
apply()
"""

df.apply(lambda x: pd.Series([x.min(), x.max()], index=['min', 'max']), axis=1).unstack()

#%%
"""
apply()
"""

df.apply(lambda x: pd.Series([x.min(), x.max()], index=['min', 'max']), axis=1).unstack().reset_index()

#%%
"""
apply()
"""

df.apply(lambda x: pd.Series([x.min(), x.max()], index=['min', 'max']), axis=1).unstack().reset_index(drop=True)

#%%
"""
apply()
"""

df.apply(lambda x: pd.Series([x.min(), x.max()], index=['min', 'max']), axis=1).unstack().reset_index(drop=True).rename(columns={0:'min', 1:'max'})

#%%
"""
apply()
"""

df.apply(lambda x: pd.Series([x.min(), x.max()], index=['min', 'max']), axis=1).unstack().reset_index(drop=True).rename(columns={0:'min', 1:'max'}).assign(range=lambda x: x['max'] - x['min'])

#%%
"""
apply()
"""

df.apply(lambda x: pd.Series([x.min(), x.max()], index=['min', 'max']), axis=1).unstack().reset_index(drop=True).rename(columns={0:'min', 1:'max'}).assign(range=lambda x: x['max'] - x['min']).drop(columns=['min', 'max'])

#%%
"""
apply()
"""

df.apply(lambda x: pd.Series([x.min(), x.max()], index=['min', 'max']), axis=1).unstack().reset_index(drop=True).rename(columns={0:'min', 1:'max'}).assign(range=lambda x: x['max'] - x['min']).drop(columns=['min', 'max']).rename(columns={'range':'range_A'})

#%%
"""
apply()
"""

df.apply(lambda x: pd.Series([x.min(), x.max()], index=['min', 'max']), axis=1).unstack().reset_index(drop=True).rename(columns={0:'min', 1:'max'}).assign(range=lambda x: x['max'] - x['min']).drop(columns=['min', 'max']).rename(columns={'range':'range_A'}).assign(range_B=lambda x: x['range_A']*100)

#%%
"""
apply()
"""

df.apply(lambda x: pd.Series([x.min(), x.max()], index=['min', 'max']), axis=1).unstack().reset_index(drop=True).rename(columns={0:'min', 1:'max'}).assign(range=lambda x: x['max'] - x['min']).drop(columns=['min', 'max']).rename(columns={'range':'range_A'}).assign(range_B=lambda x: x['range_A']*100).drop(columns=['range_A'])

#%%
"""
apply()
"""

df.apply(lambda x: pd.Series([x.min(), x.max()], index=['min', 'max']), axis=1).unstack().reset_index(drop=True).rename(columns={0:'min', 1:'max'}).assign(range=lambda x: x['max'] - x['min']).drop(columns=['min', 'max']).rename(columns={'range':'range_A'}).assign(range_B=lambda x: x['range_A']*100).drop(columns=['range_A']).rename(columns={'range_B':'range'})

#%%
"""
apply()
"""

df.apply(lambda x: pd.Series([x.min(), x.max()], index=['min', 'max']), axis=1).unstack().reset_index(drop=True).rename(columns={0:'min', 1:'max'}).assign(range=lambda x: x['max'] - x['min']).drop(columns=['min', 'max']).rename(columns={'range':'range_A'}).assign(range_B=lambda x: x['range_A']*100).drop(columns=['range_A']).rename(columns={'range_B':'range'}).assign(range_C=lambda x: x['range']*100)

#%%
"""
apply()
"""

df.apply(lambda x: pd.Series([x.min(), x.max()], index=['min', 'max']), axis=1).unstack().reset_index(drop=True).rename(columns={0:'min', 1:'max'}).assign(range=lambda x: x['max'] - x['min']).drop(columns=['min', 'max']).rename(columns={'range':'range_A'}).assign(range_B=lambda x: x['range_A']*100).drop(columns=['range_A']).rename(columns={'range_B':'range'}).assign(range_C=lambda x: x['range']*100).drop(columns=['range'])

#%%
"""
apply()
"""

df.apply(lambda x: pd.Series([x.min(), x.max()], index=['min', 'max']), axis=1).unstack().reset_index(drop=True).rename(columns={0:'min', 1:'max'}).assign(range=lambda x: x['max'] - x['min']).drop(columns=['min', 'max']).rename(columns={'range':'range_A'}).assign(range_B=lambda x: x['range_A']*100).drop(columns=['range_A']).rename(columns={'range_B':'range'}).assign(range_C=lambda x: x['range']*100).drop(columns=['range']).rename(columns={'range_C':'range'})

#%%
"""
apply()
"""

df.apply(lambda x: pd.Series([x.min(), x.max()], index=['min', 'max']), axis=1).unstack().reset_index(drop=True).rename(columns={0:'min', 1:'max'}).assign(range=lambda x: x['max'] - x['min']).drop(columns=['min', 'max']).rename(columns={'range':'range_A'}).assign(range_B=lambda x: x['range_A']*100).drop(columns=['range_A']).rename(columns={'range_B':'range'}).assign(range_C=lambda x: x['range']*100).drop(columns=['range']).rename(columns={'range_C':'range'}).assign(range_D=lambda x: x['range']*100)

#%%
"""
apply()
"""

df.apply(lambda x: pd.Series([x.min(), x.max()], index=['min', 'max']), axis=1).unstack().reset_index(drop=True).rename(columns={0:'min', 1:'max'}).assign(range=lambda x: x['max'] - x['min']).drop(columns=['min', 'max']).rename(columns={'range':'range_A'}).assign(range_B=lambda x: x['range_A']*100).drop(columns=['range_A']).rename(columns={'range_B':'range'}).assign(range_C=lambda x: x['range']*100).drop(columns=['range']).rename(columns={'range_C':'range'}).assign(range_D=lambda x: x['range']*100).drop(columns=['range'])

#%%
"""
apply()
"""

df.apply(lambda x: pd.Series([x.min(), x.max()], index=['min', 'max']), axis=1).unstack().reset_index(drop=True).rename(columns={0:'min', 1:'max'}).assign(range=lambda x: x['max'] - x['min']).drop(columns=['min', 'max']).rename(columns={'range':'range_A'}).assign(range_B=lambda x: x['range_A']*100).drop(columns=['range_A']).rename(columns={'range_B':'range'}).assign(range_C=lambda x: x['range']*100).drop(columns=['range']).rename(columns={'range_C':'range'}).assign(range_D=lambda x: x['range']*100).drop(columns=['range']).rename(columns={'range_D':'range'})

#%%
"""
apply()
"""

df.apply(lambda x: pd.Series([x.min(), x.max()], index=['min', 'max']), axis=1).unstack().reset_index(drop=True).rename(columns={0:'min', 1:'max'}).assign(range=lambda x: x['max'] - x['min']).drop(columns=['min', 'max']).rename(columns={'range':'range_A'}).assign(range_B=lambda x: x['range_A']*100).drop(columns=['range_A']).rename(columns={'range_B':'range'}).assign(range_C=lambda x: x['range']*100).drop(columns=['range']).rename(columns={'range_C':'range'}).assign(range_D=lambda x: x['range']*100).drop(columns=['range']).rename(columns={'range_D':'range'}).assign(range_E=lambda x: x['range']*100)

#%%
"""
apply()
"""

df.apply(lambda x: pd.Series([x.min(), x.max()], index=['min', 'max']), axis=1).unstack().reset_index(drop=True).rename(columns={0:'min', 1:'max'}).assign(range=lambda x: x['max'] - x['min']).drop(columns=['min', 'max']).rename(columns={'range':'range_A'}).assign(range_B=lambda x: x['range_A']*100).drop(columns=['range_A']).rename(columns={'range_B':'range'}).assign(range_C=lambda x: x['range']*100).drop(columns=['range']).rename(columns={'range_C':'range'}).assign(range_D=lambda x: x['range']*100).drop(columns=['range']).rename(columns={'range_D':'range'}).assign(range_E=lambda x: x['range']*100).drop(columns=['range'])

#%%
"""
``````