Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Split dataframe and plot subsets with a for loop in jupyter notebook

I am trying to split a dataframe and to plot the create subsets using a for loop in jupyter notebooks:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

data = pd.DataFrame(np.random.rand(120, 10))

for i in range(len(data) // 10):
    split_data = data.iloc\[i:i\*(len(data) // 10)\]
    for i, row in split_data.iterrows():
        row.plot(figsize=(15,5))
    plt.show()

Here the the figures being plotted are not reset, rather the curves of the first figure appear again on the second one and so on.

How can i reset the plot so that i have only 10 curves per plot?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your calculation in the loop in incorrect. This generates the indices:

0:0, 1:12, 2:24, 3:36, 4:48, 5:60, 6:72, 7:84, 8:96, 9:108, 10:120, 11:132

But anyway, avoid the manual indexing. Use a groupby to split your DataFrame:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

data = pd.DataFrame(np.random.rand(120, 10))

for k, split_data in data.groupby(np.arange(len(data))//10):
    split_data.plot()
    plt.show()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading