I will post about pandas from today.
A library for databases in python.
In particular A library for handling sets of data like NumPy. NumPy can treat data as a mathematical matrix and specializes in scientific calculations.
Pandas, on the other hand, can perform operations that can be done with common databases. In addition to numerical values, you can easily handle character string data such as name and address.
Data analysis can be performed efficiently by using NumPy and Pandas properly.
There are two types of data structures in Pandas: Series and DataFrame.
DataFrame It is a data structure that is mainly used and is represented by a two-dimensional table. Horizontal data is called a row, and vertical data is called a column.
Each row and each column is labeled Row label is index Column labels are called columns.
Series It is a one-dimensional array that can be thought of as a row or column in a DataFrame. Again, each element is labeled.
The index is [0, 1, 2, 3, 4]. Also, the columns are ["Prefecture", "Area", "Population", "Region"].
Series is dictionary type data ({key1: value1, key2: value2, ...}) By passing, it will be sorted in ascending order by key.
#Series data
import pandas as pd
fruits = {"orange": 2, "banana": 3}
print(pd.Series(fruits))
#Output result
banana 3
orange 2
dtype: int64
#Similarly, DataFrame is sorted by key in ascending order if columns are not specified.
#Data in DataFrame
import pandas as pd
data = {"fruits": ["apple", "orange", "banana", "strawberry", "kiwifruit"],
"year": [2001, 2002, 2001, 2008, 2006],
"time": [1, 4, 5, 6, 3]}
df = pd.DataFrame(data)
print(df)
#Output result
fruits time year
0 apple 1 2001
1 orange 4 2002
2 banana 5 2001
3 strawberry 6 2008
4 kiwifruit 3 2006
#To specify the sort order, use columns as the second argument as shown below.=[list]To specify.
import pandas as pd
df = pd.DataFrame(data, columns=["year", "time", "fruits"])
print(df)
#Output result
year time fruits
0 2001 1 apple
1 2002 4 orange
2 2001 5 banana
3 2008 6 strawberry
4 2006 3 kiwifruit
Recommended Posts