0
$\begingroup$

I've found other posts that refer to creating a new dataframe using specific conditions from another (like ID = 27, etc.) but nothing that allows me to make a new dataframe from specific row numbers of another.

Here is what I have tried so far:

To add a single row to a new dataframe:

test.append(original.loc[300])
test

To add a list of rows to a new dataframe:

entries_to_move = [10, 20, 30]
for i in entries_to_move:
   test.append(original.loc[i])
test

Neither method works, so help would be appreciated. The output for either code is just a __.

Thank you!

$\endgroup$
1
$\begingroup$

So, your question is to instantiate a new data frame df2 from another data frame df1, by simply selecting rows.

You can do this by indexing. What is great by pandas DataFrames is that you can index a DataFrame using a list of indices.

df2 = df1.iloc[[list of indices],:] 
| improve this answer | |
$\endgroup$
  • $\begingroup$ Thank you! I forgot about the end : and kept getting an empty df2. $\endgroup$ – zoomh3x Jul 3 at 1:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.