import pandas as pd
import numpy as np
list1 = [1,2,3,4,5,6]
list2 = [2,3,4,5,6,7]
D = pd.DataFrame({"m1":list1,"m2":list2})
print(D)
m1 m2
0 1 2
1 2 3
2 3 4
3 4 5
4 5 6
5 6 7
D1 = D.as_matrix()
print(D1)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-3-63694ea5efae> in <module>
----> 1 D1 = D.as_matrix()
2 print(D1)
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\generic.py in __getattr__(self, name)
5463 if self._info_axis._can_hold_identifiers_and_holds_name(name):
5464 return self[name]
-> 5465 return object.__getattribute__(self, name)
5466
5467 def __setattr__(self, name: str, value) -> None:
AttributeError: 'DataFrame' object has no attribute 'as_matrix'
D2 = D.values
print(D2)
[[1 2]
[2 3]
[3 4]
[4 5]
[5 6]
[6 7]]
D3 = D.iloc[:,:].values
print(D3)
[[1 2]
[2 3]
[3 4]
[4 5]
[5 6]
[6 7]]
D4 = D.to_excel("D.xlsx")
print(D4)
None
R1 = D.sum()
print(R1)
m1 21
m2 27
dtype: int64
R2 = D.mean()
print(R2)
m1 3.5
m2 4.5
dtype: float64
R3 = D.describe()
print(R3)
m1 m2
count 6.000000 6.000000
mean 3.500000 4.500000
std 1.870829 1.870829
min 1.000000 2.000000
25% 2.250000 3.250000
50% 3.500000 4.500000
75% 4.750000 5.750000
max 6.000000 7.000000
c1 = D.iloc[1:3,1]
print(c1)
1 3
2 4
Name: m2, dtype: int64
c2 = D.iloc[1:3,:]
print(c2)
m1 m2
1 2 3
2 3 4
TF = [True,False,False,True,True,False]
c3 = D.iloc[TF,[0]]
print(c3)
m1
0 1
3 4
4 5
c4 = D.loc[D["m1"] == 2,:]
print(c4)
m1 m2
1 2 3
import os
os.getcwd()
'C:\\Users\\Administrator'
os.chdir("C:\\Users\\Administrator\\Desktop")
path = "ss.xlsx"
data = pd.read_excel(path)
print(data)
车次 日期 上车人数
0 D02 20150101 2143
1 D02 20150102 856
2 D02 20150106 860
3 D02 20150104 1011
4 D02 20150105 807
.. ... ... ...
115 D06 20150120 1342
116 D06 20150121 1389
117 D06 20150122 1467
118 D06 20150123 1516
119 D06 20150124 1888
[120 rows x 3 columns]
path = "ss.xlsx"
data1 = pd.read_excel(path,"Sheet2")
print(data1)
车次 日期 上车人数
0 D02 20150101 2143.0
1 D02 20150102 856.0
2 D02 20150106 NaN
3 D03 20150104 1011.0
4 D02 20150105 807.0
5 D02 20150103 761.0
6 D03 20150107 803.0
7 D02 20150108 732.0
8 D02 20150109 753.0
9 D03 20150110 NaN
10 D02 20150111 694.0
11 D02 20150112 930.0
12 D03 20150113 825.0
13 D02 20150114 NaN
14 D02 20150115 802.0
15 D02 20150116 815.0
data2 = pd.read_table("txt1.txt",header = None)
print(data2)
0 1 2 3 4 5 6 7
0 8.35 23.53 7.51 8.62 17.42 10.00 1.04 11.21
1 9.25 23.75 6.61 9.19 17.77 10.48 1.72 10.51
2 8.19 30.50 4.72 9.78 16.28 7.60 2.52 10.32
3 7.73 29.20 5.42 9.43 19.29 8.49 2.52 10.00
4 9.42 27.93 8.20 8.14 16.17 9.42 1.55 9.76
5 9.16 27.98 9.01 9.32 15.99 9.10 1.82 11.35
6 10.06 28.64 10.52 10.05 16.18 8.39 1.96 10.81
7 9.09 28.12 7.40 9.62 17.26 11.12 2.49 12.65
8 9.41 28.20 5.77 10.80 16.36 11.56 1.53 12.17
9 8.70 28.12 7.21 10.53 19.45 13.30 1.66 11.96
10 6.93 29.85 4.54 9.49 16.62 10.65 1.88 13.61
11 8.67 36.05 7.31 7.75 16.67 11.68 2.38 12.88
12 9.98 37.69 7.01 8.94 16.15 11.08 0.83 11.67
13 6.77 38.69 6.01 8.82 14.79 11.44 1.74 13.23
14 8.14 37.75 9.61 8.49 13.15 9.76 1.28 11.28
15 7.67 35.71 8.04 8.31 15.13 7.76 1.41 13.25
16 7.90 39.77 8.49 12.94 19.27 11.05 2.04 13.29
17 7.18 40.91 7.32 8.94 17.60 12.75 1.14 14.80
18 8.82 33.70 7.59 10.98 18.82 14.73 1.78 10.10
19 6.25 35.02 4.72 6.28 10.03 7.15 1.93 10.39
20 10.60 52.41 7.70 9.98 12.53 11.70 2.31 14.69
21 7.27 52.65 3.84 9.16 13.03 15.26 1.98 14.57
22 13.45 55.85 5.50 7.45 9.55 9.52 2.21 16.30
23 10.85 44.68 7.32 14.51 17.13 12.08 1.26 11.57
24 7.21 45.79 7.66 10.36 16.56 12.86 2.25 11.69
25 7.68 50.37 11.35 13.30 19.25 14.59 2.75 14.87
26 7.78 48.44 8.00 20.51 22.12 15.73 1.15 16.61
27 7.94 39.65 20.97 20.82 22.52 12.41 1.75 7.90
28 8.28 64.34 8.00 22.22 20.06 15.12 0.72 22.89
29 12.47 76.39 5.52 11.24 14.52 22.00 5.46 25.50
data3 = pd.read_table("txt2.txt",sep ="\s+")
print(data3)
a b c d e
0 1 2 3 4 5
1 6 7 8 9 10
2 11 12 13 14 15
data4 = pd.read_table("txt3.txt",sep = "," ,header = None)
print(data4)
0 1 2 3 4
0 2 3 4 5 6
1 7 8 9 10 11
2 12 13 14 15 16
sss = pd.Series([1,2,3,4,5,6,7,8,9,10,11])
avgs = pd.rolling_mean(sss,3)
print(avgs)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-38-480887a633ac> in <module>
1 # 3.5 滚动计算函数
2 sss = pd.Series([1,2,3,4,5,6,7,8,9,10,11])
----> 3 avgs = pd.rolling_mean(sss,3) #新版pandas里面变化了module 'pandas' has no attribute 'rolling_mean'
4
5 print(avgs)
C:\ProgramData\Anaconda3\lib\site-packages\pandas\__init__.py in __getattr__(name)
242 return _SparseArray
243
--> 244 raise AttributeError(f"module 'pandas' has no attribute '{name}'")
245
246
AttributeError: module 'pandas' has no attribute 'rolling_mean'
sss = pd.Series([1,2,3,4,5,6,7,8,9,10,11])
avgs = sss.rolling(3).mean()
print(avgs)
0 NaN
1 NaN
2 2.0
3 3.0
4 4.0
5 5.0
6 6.0
7 7.0
8 8.0
9 9.0
10 10.0
dtype: float64
sum_sss = sss.rolling(3).sum()
print(sum_sss)
0 NaN
1 NaN
2 6.0
3 9.0
4 12.0
5 15.0
6 18.0
7 21.0
8 24.0
9 27.0
10 30.0
dtype: float64
max_sss = sss.rolling(3).max()
print(max_sss)
0 NaN
1 NaN
2 3.0
3 4.0
4 5.0
5 6.0
6 7.0
7 8.0
8 9.0
9 10.0
10 11.0
dtype: float64
|