Indexing and Selecting Data in Python – How to slice, dice for Pandas Series and DataFrame

guest_blog 02 Sep, 2022 • 5 min read

Introduction

Indexing and Selecting Data

  • Enables automatic and explicit data alignment.
  • Allows intuitive getting and setting of subsets of the data set.

 

# for getting values with a boolean array
print (df.loc['a']>0)

indexing and selecting data - .loc

print df.loc[:,'B']

indexing and selecting data - .loc

The query() Method

#creating dataframe of 10 rows and 3 columns
df4 = pd.DataFrame(np.random.rand(10, 3), columns=list('abc'))
df4

Image for post

Image for post

#with query()
df4.query('(x < b) & (b < c)')

Image for post

  • drop_duplicates: removes duplicate rows.
df5 = pd.DataFrame({'a': ['one', 'one', 'two', 'two', 'two'],
                    'b': ['x', 'y', 'x', 'y', 'x'],
                    'c': np.random.randn(5)})
df5

Image for post

df5.duplicated('a')

Image for post

df5.drop_duplicates('a')

Image for post

  1. Interesting 10 Machine Learning and Data Science Projects with Datasets
  2. Basic Understanding of NLP With Python

 

About the Author

Author

Amit Chauhan

I am a Research Scholar and a technical person with 4-year experience in R&D Electronics. Data Science enthusiastic
Data Indexingpandaspython
guest_blog 02 Sep 2022

BeginnerData ExplorationPandasProgrammingPython

Frequently Asked Questions

Lorem ipsum dolor sit amet, consectetur adipiscing elit,

Responses From Readers

Clear

Related Courses

image.name
0 Hrs 27 Lessons
4.86

Pandas for Data Analysis in Python

Free

Python
Become a full stack data scientist
  • [tta_listen_btn class="listen"]