Search code examples
pythonpandasdataframedata-analysis

Pandas dataframe row extraction is changing dimensions


I have a [2 rows x 51 columns] dataframe read into a variable called "csv". I want to create 2 new csv's from this, row based. The first csv becomes csv_row1 which will be [1 row x 51 columns] The second csv becomes csv_row2 which will be another [1 row x 51 columns].

I wrote just one line of code:

firstrow = csv.loc[0]
firstrowindex = len(firstrow.index)
print(firstrowindex)

#output 51 

So, while I was expecting to view a [1 row x 51 columns] dataframe, I instead saw a [51 rows x 2 columns]

How can I do this differently? How can I extract a whole row of data and create a separate DataFrame for just that 1 row? And do this for each row?


Solution

  • To get row as a DataFrame you need to use:

    csv_row1 = csv.loc[[0]]