Home > OS >  How can I choose two different rows of a DataFrame and have them in a new DataFrame?
How can I choose two different rows of a DataFrame and have them in a new DataFrame?

Time:10-10

What is simplest way to choose two different rows of a dataframe?

tx_df is the original dataset, and toy_df is the subset which is supposed to contain only two rows of the original dataset.

toy_df = tx_df.loc[tx_df['CustomerID'] == (14566) ]
toy_df = tx_df.loc[tx_df['CustomerID'] == (17844) ]

Thank you

CodePudding user response:

toy_df  = tx_df[tx_df$CustomerID %in% c(14566, 17884),]

CodePudding user response:

Using .query

toy_df = tx_df.query("CustomerID.isin([14566, 17844])")

CodePudding user response:

tx_df.set_index('CustomerID').loc[[14566, 17844]]
tx_df[tx_df.CustomerID.isin([14566, 17844])]
tx_df.query('CustomerID == 14566 or CusromerID == 17844')

P.S. I think that in this case the easiest way to locate records by CustomerID would be to use the latter as an index (see the first line in the code above).

  • Related