hello 大家好,长时间没有发布新的文章,证明小编在这段日子不是很忙,就是很堕落,显然是后者了
捡起键盘,准备推出pandas 案例100系列,分十次记录学习,多了你也看不进去,我也写不进去:
案例1:
生成一年中,所有周一的日期:
1 2 3 4 5
| import pandas as pd date_range = pd.date_range(start='2021-01-01', end='2021-12-31', freq="W-MON") date_range = pd.date_range(start='2021-01-01', periods=52, freq="W-MON") print(date_range)
|
案例2:
生成一个月份的所有日期
1 2 3 4
| date_range = pd.date_range(start='2021-12-01', end='2021-12-31') date_range = pd.date_range(start='2021-12-01', periods=31) print(date_range)
|
案例3:
生成一天的所有小時
1 2 3 4
| date_range = pd.date_range(start='2021-12-01', periods=24, freq="H") date_range = pd.date_range(start='2021-12-01', end='2021-12-02', closed="left", freq="H") print(date_range)
|
案例4:
使用list构造 Series
1 2 3 4 5
| import pandas as pd
sites = ["Google", "Runoob", "Wiki"] myvar = pd.Series(sites) print(myvar)
|
案例5:
使用dict 构造 Series
1 2 3 4
| sites = {'x': "Google", 'y': "Runoob", 'z': "Wiki"} myvar = pd.Series(sites) print(myvar)
|
案例6:
Series转换list
1 2 3 4 5 6
| import pandas as pd
sites = {'x': "Google", 'y': "Runoob", 'z': "Wiki"} myvar = pd.Series(sites) data = myvar.tolist() print(data)python
|
案例7:
将Series转换DataFrame
1 2 3 4 5 6
| import pandas as pd
grades = {'语文': "100", '数学': "120", '英语': "60"} myvar = pd.Series(grades) data = pd.DataFrame(myvar, columns=["grade"]) print(data)
|
案例8:
用numpy 创建Series
101 10.0
102 20.0
103 30.0
104 40.0
105 50.0
106 60.0
107 70.0
108 80.0
109 90.0
1 2 3 4 5 6 7 8 9
| import pandas as pd import numpy as np
s = pd.Series( np.arange(10, 100, 10), index=np.arange(101, 110), dtype="float" ) print(s)
|
案例9:
转换Series 的数据类型
1 2 3 4 5 6 7 8 9 10 11 12 13
| import pandas as pd import numpy as np
s = pd.Series( data=["001", "002", "003", "004"], index=list("abcd"), dtype="float" )
s = s.astype(int)
print(s)
|
案例10:
给Series添加元素
1 2 3 4 5 6 7 8 9 10
| import pandas as pd
grades = {'语文': "100", '数学': "120", '英语': "60"} data = pd.Series(grades) data = data.append(pd.Series({ "计算机": 30, "化学": 45 })) print(data)
|
案例11:
用reset index将Series 转换成df
1 2 3 4 5 6 7
| import pandas as pd
grades = {'语文': "100", '数学': "120", '英语': "60"} data = pd.Series(grades) df = data.reset_index() df.columns = ["course", "grade"] print(data)
|