PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, November 2, 2022

[FIXED] How can I choose two different rows of a DataFrame and have them in a new DataFrame?

 November 02, 2022     dataframe, indexing, pandas     No comments   

Issue

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


Solution

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).



Answered By - Vitalizzare
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing