Basic usage of Pandas Summary

As a reminder of how to use Pandas The basic operation method is summarized.

0. Premise

import pandas as pd

1. Define the data

pd.DataFrame() --Methods that can define data frames --You can specify the index argument. If not specified, it will be automatically assigned from 0

df = pd.DataFrame({
'Country': ['JPN', 'USA', 'CHI', 'GER', 'AUS'],
'Greeting':['Hello', 'Hello', 'Ni Hao', 'Guten Tag', 'GDay'],
'Capial':['Tokyo','Washington', 'Beijing', 'Berlin', 'Canberra']},
index=['a','b','c','d','e']
)

df

"""
The output is as follows
	Country	Greeting	Capial
a JPN Hello Tokyo
b	USA	Hello	Washington
c	CHI	Ni Hao	Beijing
d	GER	Guten Tag	Berlin
e	AUS	GDay	Canberra
"""

2. How to operate the data frame

Confirmation of data existence

df.isin () method

--Take an array (['hage','hige','huge' ...]) as an argument and return the presence or absence of that value as a boolean value.

df.isin(['JPN', 'Berlin'])

"""
Country	Greeting	Capial
a	True	False	False
b	False	False	False
c	False	False	False
d	False	False	True
e	False	False	False

"""

df.isnull () method

--Checks for missing values (NaN) and returns a boolean value (True means NaN).

b. Calling data

df.loc[] --Specify rows and columns by label (name) --It is necessary to specify each row and column for the subscript ([]). `` `: ``` means all parts

df.loc[:,['Country', 'Greeting']] 
#Will:'Country'Column,'Greeting'Get the columns. All lines

df.loc[['a','c'],['Country']] # ->
#Will:'Country'Get the columns. Only lines a and c

df.iloc[] --Specify the row / column by the integer location --By the way, i is neither index nor indice nor iterator, but *** i *** of *** i *** ntegra *** loc *** ation ('integer position') [^ 1].

df.iloc[:,1:3]
#Will:1st to 3rd row(That is, the first row and the second column)Output, all lines

df.iloc[2:5,1:3]
#Will:1st to 3rd row(same)Output the lines from the 2nd line to the 5th line(That is 2,3,4th line)

"""
	Greeting	Capial
c	Ni Hao	Beijing
d	Guten Tag	Berlin
e	GDay	Canberra
"""

df.ix[] --It works regardless of whether you call the label (loc) or the integer number (iloc). Deprecated from Pandas version 0.20.0 [^ 2]. ――I think it's enough to know that there was such a thing.

Editor's Note

--How to delete columns and query-like utilization will be added soon. ――Because it is one of the most basic libraries along with Numpy and Matplotlib, I thought that it is a library that requires review so that you do not stumble on the operation method. I hope it helps similar people.

Articles that I used as a reference

-Get a specific row / column from a dataframe in Pandas -Differences between pandas loc, iloc and ix – python

Recommended Posts

Basic usage of Pandas Summary
Basic operation of pandas
Summary of pyenv usage
Basic usage of flask-classy
Basic usage of Jinja2
Basic operation of Pandas
Basic usage of SQLAlchemy
pandas Matplotlib Summary by usage
Basic usage of PySimple GUI
Convenient usage summary of Flask
Basic usage of Python f-string
Index of certain pandas usage
Make a note of the list of basic Pandas usage
Summary of basic knowledge of PyPy Part 1
Summary of basic implementation by PyTorch
[Python] Summary of how to use pandas
Basic usage of Btrfs on Arch Linux
pytest usage summary
Summary of what was used in 100 Pandas knocks (# 1 ~ # 32)
[Introduction to Python] Basic usage of lambda expressions
Basic operation of Python Pandas Series and Dataframe (1)
Numerical summary of data
Pandas Personal Notes Summary
Usage of Python locals ()
[Linux] Basic command summary
Summary of string operations
Basic knowledge of Python
Summary of Python arguments
Summary of logrotate software logrotate
Summary of test method
Basic processing of librosa
Python Basic --Pandas, Numpy-
Python --Explanation and usage summary of the top 24 packages
Summary of pre-processing practices for Python beginners (Pandas dataframe)
Summary of Pandas methods used when extracting data [Python]
(Beginner) Basic usage of Datastore on Google App Engine
[Python] Summary of table creation method using DataFrame (pandas)
Summary of the basic flow of machine learning with Python
Summary of things that were convenient when using pandas
[Introduction to Python] Basic usage of the library matplotlib
Basic summary of data manipulation in Python Pandas-Second half: Data aggregation
[Python] Correct usage of map
Python application: Pandas Part 1: Basic
Summary of python file operations
Summary of Python3 list operations
2017.3.6 ~ 3.12 Summary of what we did
Formatted display of pandas DataFrame
Summary of Linux distribution types
Basic flow of anomaly detection
virtualenv Basic command usage memo
Pipenv usage summary (for myself)
python pandas study recent summary
(Minimal) usage of django logger
XPath Basics (1) -Basic Concept of XPath
Sample usage of Python pickle
One-liner basic graph of HoloViews
Behavior of pandas rolling () method
A brief summary of Linux
[Python] Correct usage of join
Basic summary of data manipulation with Python Pandas-First half: Data creation & manipulation
Summary of Proxy connection settings