1.numpy
2.pandas
Series:
s=pd.Series(np.arange(3))
输出:
0 0
1 1
2 2
dtype: int32
替换索引列:
s=pd.Series(np.arange(3),index=['a','b','c'])
DataFrame:
从df中提取两列生成新的DataFrame:
df_new = DataFrame(df, columns = ['May 2018', 'Change.1'])
给某一列赋值:
df_new['Sep 2019'] = np.arange(1,11)
df_new['Sep 2019'] = range(0,10)
df_new['Sep 2019'] = pd.Series(np.arange(2,12))
某一列的两行元素赋值:
df_new['Sep 2019'] = pd.Series([100,200], index=[1,2])
构建DataFrame并且转至
>>> s1 = pd.Series(data['Student'], name = 'Student', index = ['one','two','three'])
>>> s2 = pd.Series(data['Grade'], name = 'Grade', index = ['one','two','three'])
>>> s3 = pd.Series(data['Class'], name = 'Class', index = ['one','two','three'])
>>> df2 = pd.DataFrame([s1,s2,s3])
>>> df2
>>> df2
one two three
Student XiaoMing XiaoHong XiaoWang
Grade 100 90 20
Class RG1 RG2 RG3
# 用列表构建是按行放置的,需要装置一下
>>> df2.T
Student Grade Class
one XiaoMing 100 RG1
two XiaoHong 90 RG2
three XiaoWang 20 RG3
转为excle或者json存储:
df1.to_csv('df1.csv')
df1.to_json('df1.json')
DataFrame的索引,切片
df1['名称']
df1.iloc[1:5,2:4] #1-4行,2-3列
df1.iloc['a':'c','行名1':'行名2']