pandas self-study notes

install


#install
pip install pandas

Series is a one-dimensional data structure.

Series


#Created with random
arr = np.random.randn(6)
s1 = pd.Series(arr)

#Created with arange
arr2 = np.arange(6)
s2 = pd.Series(arr2)

#Created with ones
arr3 = np.ones(6)
s3 = pd.Series(arr3)

#Created with zeros
arr4 = np.zeros(6)
s4 = pd.Series(arr4)

#index specification
arr = np.random.randn(6)
s5 = pd.Series(arr, index=["January", "February", "March", "April", "May", "June"])
#* It is okay to duplicate indexes

#Created with a dictionary
dict_data = {"January": 11, "February": 13, "March": 10, "April": 14, "May": 8, "June": 10}
s6 = pd.Series(dict_data)

###############################################
#Data acquisition by index
s6[0]

#Get data with key
s3["January"]

DataFrame is a two-dimensional data structure.

DataFrame


import pandas as pd
import numpy as np

#Created with numpy
arr = np.random.randn(20).reshape((4, 5))
df = pd.DataFrame(arr)

#Row and column index specification
index = ["Q1", "Q2", "Q3", "Q4"]
columns = ["A", "B", "C", "D", "E"]
df = pd.DataFrame(arr, index=index, columns=columns)

###################################################
#How to use
#Get row
df.index
#Get column
df.columns
#Data acquisition
df.values
#get describe
df.describe()
#count Number of data
#unique Number of unique (unique) values
#top Most frequently occurring value (mode)
#The number of occurrences of freq mode
#mean mean
#std standard deviation
#min minimum value
#25%      1/Quartile
#50%Median
#75%      3/Quartile
#max maximum value

#sort
#index sort
df.sort_index(axis=0, ascending=False)
#※axis:Line 0,1 column ascending: true ascending order,false descending order

#Descending in row A
df.sort_values(axis=0, by="A", ascending=False)

#Ascending order in Q3 line
df.sort_values(axis=1, by="Q3", ascending=True)

##############################################
#Data acquisition
#Get row data
df.loc["Q3"]
df.loc["Q1":"Q3"]
df.iloc[0]
df.iloc[0:3]

#Column data acquisition
df.loc[:,"B"]
df.loc["Q2","A":"C"]
df.iloc[:, 0]
df.iloc[0:2, 0:3]

#Matrix data acquisition
df.ix["Q0":"Q2", 1:3]

############################################
#Check empty data
df.isnull()

#Matrix deletion of empty data
df.dropna(axis=0, how="any")
#※axis:0 rows 1 columns how:"any"Delete if there is,"all"Delete all nan

#Set empty data to 1
df.fillna(value=1)

#############################################
#Data join
#concat method
pd.concat([df1, df2, df3,...],
           axis=0 or 1,
           ignore_index=True,
           join="outer" or "inner",  
           join_axes=[data1.index])
#          df1 :DataFrame
#axis: Join direction
#      ignore_index: Sort index(default:False)
#join: outer direct sum inner union
#          join_axes: Join reference data
index1 = pd.date_range("2018-10-01", periods=4)
df1 = pd.DataFrame(np.arange(20).reshape((4, 5)), index=index1, columns=list("ABCDE"))
index2 = pd.date_range("2018-10-03", periods=3)
df2 = pd.DataFrame(np.arange(12).reshape((3, 4)), index=index2, columns=list("DEFG"))

# axis=0 Combine vertically
result = pd.concat([df1, df2], axis=0, sort=True)
# axis=1 Combine horizontally
result2 = pd.concat([df1, df2], axis=1)
# axis=0, join_axes=[df1.columns]
result6 = pd.concat([df1, df2], axis=0, join_axes=[df1.columns], sort=False)
# axis=1, join_axes=[df1.index]
result7 = pd.concat([df1, df2], axis=1, join_axes=[df1.index], sort=False)

#df1.append()method
df1.append([df2, df2, df3,...],
           axis=0 or 1,
           ignore_index=True)
result8 = df1.append(df2, ignore_index=True, sort=False)

#pd.merge()method
pd.merge(left, right,
         on=["key1", "key2"],
         how="inner" or "outer" or "left" or"right"
         indicator="Explanation")

left = pd.DataFrame({
    "key": ["A0", "A1", "A2", "A3"],
    "B": ["B0", "B1", "B2", "B3"],
    "C": ["C0", "C1", "C2", "C3"],
})

right = pd.DataFrame({
    "key": ["A0", "A1", "A2", "A3"],
    "D": ["B1", "B2", "B2", "B4"],
    "E": ["C2", "C1", "C5", "C2"]
})

# "key"Combine with
result9 = pd.merge(left, right, on="key")
result10 = pd.merge(left, right, on=["key", "B"])

result11 = pd.merge(left, right, on=["key", "B"])
result12 = pd.merge(left, right, on=["key", "B"], how="left")
result11 = pd.merge(left, right, on=["key", "B"], how="right")
#how =left: Combine all left with hold

################################################
#Data output
left = pd.DataFrame({
    "key": ["A0", "A1", "A2", "A3"],
    "B": ["B0", "B1", "B2", "B3"],
    "C": ["C0", "C1", "C2", "C3"],
})
# csv
left.to_csv("left_data.csv")
# json
left.to_json("left.json")
#others
#to_json,to_excel,to_csv,to_clipboard,
#to_dense,to_dict,to_feather,to_gbq,
#to_hdf,to_html,to_latex,to_msgpack,
#to_panel,to_parquet,to_pickle,to_records
#to_sparse,to_sql,to_stata

#########################################
#Graph
import matplotlib.pyplot as plt
s = pd.Series(np.random.randn(1000))
result = s.cumsum()
result.plot()
plt.show()


Recommended Posts

pandas self-study notes
Pandas notes
python pandas notes
Pandas Personal Notes Summary
Pandas
Python Pandas Data Preprocessing Personal Notes
Pandas memo
JetBrains_Learning Notes_003
SQLAlchemy notes
pyenv notes
Pandas basics
SQL notes
Sphinx notes
django notes
Jupyter_Learning Notes_000
Pandas memorandum
Pandas basics
pandas memorandum
pandas memo
pandas SettingWithCopyWarning
Django notes