Basic introduction to pandas
-
NaN (Not a Number) is a type of value of numerical data type in computer science, which represents an undefined or unrepresentable value.
-
pd.set_option('display.width', None)
Show omitted parts
Series; DataFrame; description and sorting
import pandas as pd
import numpy as np
#创建一个序列; np.nan 空
s=pd.Series([1,3,6,np.nan,44,1])
print(s)
#创建一个dataform
dates = pd.date_range('20200101',periods=6)
print(dates)
# DateFrame 大的matrix; 类似于二维的numpy; index定义行,colunms定义列
df = pd.DataFrame(np.random.randn(6,4),index=dates,columns=['a','b','c','d'])
print(df)
df1=pd.DataFrame(np.arange(12).reshape((3,4)))
print(df1)
df2 = pd.DataFrame({
'A':1.,
'B':pd.Timestamp('20130102'),
'C':pd.Series(1,index=list(range(4)),dtype='float32'),
'D':np.array([3]*4,dtype='int32'),
'E':pd.Categorical(['test','train','test','train']),
'F':'foo'})
print(df2)
print(df2.dtypes)
print(df2.index)
print(df2.columns)
print(df2.values)
print(df2.describe()) #日期和字符串不显示,describe只运算数字型式
pd.set_option('display.width', None)
print(df2.T)
# 排序
print(df.sort_index(axis=1,ascending=False))
print(df.sort_index(axis=0,ascending=False))
print(df2.sort_values(by='E'))
return
0 1.0
1 3.0
2 6.0
3 NaN
4 44.0
5 1.0
dtype: float64
DatetimeIndex(['2020-01-01', '2020-01-02', '2020-01-03', '2020-01-04',
'2020-01-05', '2020-01-06'],
dtype='datetime64[ns]', freq='D')
a b c d
2020-01-01 0.317569 -0.253862 -0.517981 -1.503216
2020-01-02 1.204460 0.075448 0.856342 -0.163644
2020-01-03 -0.660971 2.310299 -1.301749 -0.897437
2020-01-04 1.545248 0.252471 0.962976 -0.694375
2020-01-05 -0.137885 1.855668 -0.992026 -1.242994
2020-01-06 -0.346280 0.758596 1.876003 -1.093228
0 1 2 3
0 0 1 2 3
1 4 5 6 7
2 8 9 10 11
A B C D E F
0 1.0 2013-01-02 1.0 3 test foo
1 1.0 2013-01-02 1.0 3 train foo
2 1.0 2013-01-02 1.0 3 test foo
3 1.0 2013-01-02 1.0 3 train foo
A float64
B datetime64[ns]
C float32
D int32
E category
F object
dtype: object
Int64Index([0, 1, 2, 3], dtype='int64')
Index(['A', 'B', 'C', 'D', 'E', 'F'], dtype='object')
[[1.0 Timestamp('2013-01-02 00:00:00') 1.0 3 'test' 'foo']
[1.0 Timestamp('2013-01-02 00:00:00') 1.0 3 'train' 'foo']
[1.0 Timestamp('2013-01-02 00:00:00') 1.0 3 'test' 'foo']
[1.0 Timestamp('2013-01-02 00:00:00') 1.0 3 'train' 'foo']]
A C D
count 4.0 4.0 4.0
mean 1.0 1.0 3.0
std 0.0 0.0 0.0
min 1.0 1.0 3.0
25% 1.0 1.0 3.0
50% 1.0 1.0 3.0
75% 1.0 1.0 3.0
max 1.0 1.0 3.0
0 1 2 3
A 1 1 1 1
B 2013-01-02 00:00:00 2013-01-02 00:00:00 2013-01-02 00:00:00 2013-01-02 00:00:00
C 1 1 1 1
D 3 3 3 3
E test train test train
F foo foo foo foo
d c b a
2020-01-01 -1.503216 -0.517981 -0.253862 0.317569
2020-01-02 -0.163644 0.856342 0.075448 1.204460
2020-01-03 -0.897437 -1.301749 2.310299 -0.660971
2020-01-04 -0.694375 0.962976 0.252471 1.545248
2020-01-05 -1.242994 -0.992026 1.855668 -0.137885
2020-01-06 -1.093228 1.876003 0.758596 -0.346280
a b c d
2020-01-06 -0.346280 0.758596 1.876003 -1.093228
2020-01-05 -0.137885 1.855668 -0.992026 -1.242994
2020-01-04 1.545248 0.252471 0.962976 -0.694375
2020-01-03 -0.660971 2.310299 -1.301749 -0.897437
2020-01-02 1.204460 0.075448 0.856342 -0.163644
2020-01-01 0.317569 -0.253862 -0.517981 -1.503216
A B C D E F
0 1.0 2013-01-02 1.0 3 test foo
2 1.0 2013-01-02 1.0 3 test foo
1 1.0 2013-01-02 1.0 3 train foo
3 1.0 2013-01-02 1.0 3 train foo
Process finished with exit code 0