Hackerss.com

hackerss
hackerss

Posted on • Updated 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)

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

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

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

df.apply(np.sum)

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

df.applymap(lambda x: x*100)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)