Search code examples
pythonpandasdataframematplotlibscatter-plot

Loop through all columns of dataframe to plot scatter plot for all columns


My dataframe has 6 columns, I would like to plot scatter plot for each column against other columns, is there way to loop through all columns of dataframe to plot scatter plot rather than manually selecting x and y ?

Example

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(100, 6), columns=['a', 'b', 'c', 'd', 'e', 'f'])
df.head()

# I don't want to manually select x and y instead, I am looking for automatically selecting one columns with another column

df.plot(kind='scatter', x='a', y='b', color='r')    
df.plot(kind='scatter', x='a', y='c', color='r')    
df.plot(kind='scatter', x='a', y='d', color='r')   
df.plot(kind='scatter', x='a', y='e', color='r')   
df.plot(kind='scatter', x='a', y='f', color='r') 

Solution

  • Of course, you can iterate over columns like this

    for column in df:
        print(df[column])