Python做数据处理的测试代码

1
2
3
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
1
2
3
4
5
s = pd.Series([1, 3, 5, np.nan, 6, 8])
dates = pd.date_range('20160515', periods=6)
df = pd.DataFrame(np.random.randn(6, 5), index=dates, columns=list('ABCDE'))
A B C D E
2016-05-15 1.212443 1.951539 -1.267197 0.724957 -0.213251
2016-05-16 1.330359 -0.027862 -1.165301 0.375969 0.164395
2016-05-17 0.149371 -0.968143 1.170630 -0.746894 -3.195635
2016-05-18 -0.539763 -1.969229 -0.027755 0.544540 -0.454799
2016-05-19 0.805278 -0.352158 0.957211 0.300742 0.254079
2016-05-20 -0.408031 -0.463302 -0.741122 0.189913 -0.425284
1
df.values
array([[ 1.21244274,  1.95153857, -1.26719682,  0.72495701, -0.21325095],
       [ 1.33035857, -0.02786182, -1.16530075,  0.37596933,  0.16439501],
       [ 0.14937056, -0.96814324,  1.17063035, -0.74689396, -3.19563471],
       [-0.53976317, -1.96922943, -0.02775519,  0.54453967, -0.45479851],
       [ 0.80527757, -0.3521584 ,  0.95721072,  0.30074246,  0.25407928],
       [-0.40803089, -0.46330215, -0.74112219,  0.18991253, -0.42528389]])
1
df[df > 0]
A B C D E
2016-05-15 1.212443 1.951539 NaN 0.724957 NaN
2016-05-16 1.330359 NaN NaN 0.375969 0.164395
2016-05-17 0.149371 NaN 1.170630 NaN NaN
2016-05-18 NaN NaN NaN 0.544540 NaN
2016-05-19 0.805278 NaN 0.957211 0.300742 0.254079
2016-05-20 NaN NaN NaN 0.189913 NaN
1
2
3
df2= df.copy()
df2['E'] = ['one', 'two', 'three', 'four', 'five', 'six']
A B C D E
2016-05-15 1.212443 1.951539 -1.267197 0.724957 one
2016-05-16 1.330359 -0.027862 -1.165301 0.375969 two
2016-05-17 0.149371 -0.968143 1.170630 -0.746894 three
2016-05-18 -0.539763 -1.969229 -0.027755 0.544540 four
2016-05-19 0.805278 -0.352158 0.957211 0.300742 five
2016-05-20 -0.408031 -0.463302 -0.741122 0.189913 six
1
2
3
4
5
6
7
8
9
10
11
m = pd.Series(np.random.randint(0, 8, size=8))
ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000))
ts = ts.cumsum()
ds = pd.DataFrame(np.random.randn(1000, 4), index = ts.index, columns=['A', 'B', 'C', 'D'])
ds = ds.cumsum()
ts.plot()
ds.plot()
plt.show()

png

png

1
ds.to_csv('foo.csv')
1
pd.read_csv("test.csv")
------ 本文结束 ------