0

I am aware of:

for index, row in dataframe.iterrows():

But I wish for each row to be its own dataframe instead of the type series. How would I go about doing this? Do I have to convert it or this a better way of looping through?

OultimoCoder
  • 244
  • 2
  • 7
  • 24
  • 3
    The best is to not loop (if possible). If you provide a [mcve] with sample data, a clear statement of your problem, and the expected output you will get good advice on how to best solve your problem. – ALollz Apr 26 '19 at 14:53
  • 1
    You never loop. Use vectorization instead. – Ricky Kim Apr 26 '19 at 14:55
  • @ALollz How could my problem description be clearer? And how would additional code help? I have a dataframe and I wish to access it row by row with each row being its own dataframe. Am I supposed to write the code for reading in my csv file to a dataframe? Surely that is unrelated to the problem and the example code I did put there would give people the idea of what I was looking for? – OultimoCoder Apr 26 '19 at 14:59
  • @OultimoCoder you could explicitly say which one is your goal here. From what we read it looks like you are aiming for a slow and not scalable solution. – rpanai Apr 26 '19 at 15:01
  • 1
    Your statement is clear, but this is likely an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). We can help you loop through the DataFrame, though this is typically something you avoid in `pandas`, see https://stackoverflow.com/a/55557758/4333359. What would be more useful is to explain the problem you need to solve (the one that "requires" iterrows), and then you'd get an answer to that, instead of a way to do something you should be avoiding in the first place. – ALollz Apr 26 '19 at 15:09

4 Answers4

2

If you need each row as DataFrame

for i in range(len(df)):
    df.iloc[[i],:]

for index in df.index : 
    df.loc[[index],:]
BENY
  • 317,841
  • 20
  • 164
  • 234
0

I would suggest loading the dataframes into a list of dataframes, then you can access them individually.

df_list = []
for index, row in df.iterrows():
    df_list.append(df[index:index+1])

then you can access the list df_list[0] for example

Chris Macaluso
  • 1,372
  • 2
  • 14
  • 33
0

You can use numpy.split

dfs=np.split(df,list(range(1,len(df))),axis=0)
Ricky Kim
  • 1,992
  • 1
  • 9
  • 18
0

Depends on the usage. From what I could guess (a function that only applies on a DataFrame), you have two options :

Option 1

convert your row to a frame :

df_row = row.to_frame()

Option 2

group your df (doing something as silly as resetting its index to have uniquely dataFrames of only 1 row), and apply the function to it :

df.reset_index().groupby('index_0').apply(func)