'Fragmenting Data from a Data Frame
So I am just starting out with Python and have a confusing task at hand. I have this dataframe of 100 elements with an column of 'counter' going from 1 to 10, and repeating 10 times.
Now, I want to split this dataframe into 10 different dataframes based on the counter. I have tried using for loop and conditional statements to reset the loop when the counter goes back to 1, but then I am losing on the 10th element.
I hope I am able to explain my problem well.
Kindly guide me. I just need a direction about how to proceed in a situation like this.
Cheers!
Solution 1:[1]
import pandas as pd
# creating mock df
df = pd.DataFrame([{'counter':e,'col_name':'some_value'} for e in
range(1,11)])
df = pd.concat([df,df]).reset_index()
#slicing the df at every 10nth index
splitted_dfs = [df.iloc[e-10:e,:] for e in range(10,len(df)+1,10)]
You are probably loosing the 10th element because range(x,y) includes y-1 and not y.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
