熊猫作为pd导入 进口numpy np
#创建DataFrame对象 df=pd。DataFrame([1、2、3、4、5),列=肮乜凇?指数=[a, b, c, d ', ' e ']) 打印df
关口 一个1 b 2 c 3 d 4 e 5
df2=pd。DataFrame([[1, 2, 3],[4、5、6]],列=[‘col1’,‘col2 ', ' col3 '],指数=[a, b]) 打印df2
col1 col2 col3 1 2 3 4 5 6 b
df3=pd.DataFrame (np.array([[1、2],[3,4]]),列=[' col1 ', ' col2 '],指数=[a, b]) 打印df3
col1 col2 1 2 b 3 4
df4=pd.DataFrame ({col1: [1,3],“col2”:(2、4)},指数=[a, b]) 打印df4
col1 col2 1 2 b 3 4
创建DataFrame对象的数据可以为列表,数组和字典,列名和索引为列表对象
# DataFrame对象的基本操作 df2.index
指数([u ' a ' u ' b '], dtype='对象')
df2.columns
指数([u 'col1”、u 'col2”u 'col3”], dtype=岸韵蟆?
#根据索引查看数据 df2.loc [a] #索引为一这一行的数据 # df2。iloc[0]跟上面的操作等价,一个是根据索引名,一个是根据数字索引访问数据
col1 - 1 col2 2 col3 3 名称:dtype: int64
打印df2。疯狂的[[a, b]] #访问多行数据,索引参数为一个列表对象
col1 col2 col3 1 2 3 4 5 6 b
打印df.loc [df.index [1:3]]
关口 b 2 c 3
#访问列数据 打印df2 [[‘col1’,‘col3]]
col1 col3 1 3 b 4 6
# DataFrame元素求和 #默认是对每列元素求和 打印df2.sum ()
col1 5 col2 7 col3 9 dtype: int64
#行求和 打印df2.sum (1)
一个6 b 15 dtype: int64
#对每个元素乘以2 打印df2。应用(λx: x * 2)
col1 col2 col3 一个2 4 6 8 10 12 b
#对每个元素求平方(支持ndarray一样的向量化操作) 打印df2 * * 2
col1 col2 col3 1 4 9 b 16 25 36
列扩充 #对DataFrame对象进行列扩充 df2 [' col4 ']=[“cnn”、“rnn”] 打印df2
col1 col2 col3 col4 1 2 3 cnn 4 5 6 b rnn
#也可以通过一个新的DataFrame对象来定义一个新列,索引自动对应 df2 [' col5 ']=pd.DataFrame ([' MachineLearning ', ' DeepLearning '],指数=[a, b]) 打印df2
col1 col2 col3 col4 col5 1 2 3 cnn MachineLearning 4 5 6 b rnn DeepLearning
#行进行扩充 打印df2.append (pd.DataFrame ({“col2”:“col1”: 7日,8日“col3”: 9,“col4”:“rcnn”,“col5”:“ReinforcementLearning”},指数=[' c ']))
col1 col2 col3 col4 col5 1 2 3 cnn MachineLearning 4 5 6 b rnn DeepLearning c 7 8 9 rcnn ReinforcementLearning
#如果在进行行扩充时候没有,指定指数的参数,索引会被数字取代 打印df2.append ({“col2”:“col1”: 10日11日“col3”: 12日“col4”:“frnn”,“col5”:“DRL”}, ignore_index=True)熊猫:DataFrame对象的基础操作方法