import pandas as pd
import numpy as np
创建DataFrame对象
# 创建DataFrame对象
df = pd.DataFrame([1, 2, 3, 4, 5], columns=['cols'], index=['a','b','c','d','e'])
print df
cols
a 1
b 2
c 3
d 4
e 5
df2 = pd.DataFrame([[1, 2, 3],[4, 5, 6]], columns=['col1','col2','col3'], index=['a','b'])
print df2
col1 col2 col3
a 1 2 3
b 4 5 6
df3 = pd.DataFrame(np.array([[1,2],[3,4]]), columns=['col1','col2'], index=['a','b'])
print df3
col1 col2
a 1 2
b 3 4
df4 = pd.DataFrame({'col1':[1,3],'col2':[2,4]},index=['a','b'])
print df4
col1 col2
a 1 2
b 3 4
创建DataFrame对象的数据可以为列表,数组和字典,列名和索引为列表对象
基本操作
# DataFrame对象的基本操作
df2.index
Index([u'a', u'b'], dtype='object')
df2.columns
Index([u'col1', u'col2', u'col3'], dtype='object')
# 根据索引查看数据
df2.loc['a']
# 索引为a这一行的数据
# df2.iloc[0] 跟上面的操作等价,一个是根据索引名,一个是根据数字索引访问数据
col1 1
col2 2
col3 3
Name: a, dtype: int64
print df2.loc[['a','b']] # 访问多行数据,索引参数为一个列表对象
col1 col2 col3
a 1 2 3
b 4 5 6
print df.loc[df.index[1:3]]
cols
b 2
c 3
# 访问列数据
print df2[['col1','col3']]
col1 col3
a 1 3
b 4 6
计算
# DataFrame元素求和
# 默认是对每列元素求和
print df2.sum()
col1 5
col2 7
col3 9
dtype: int64
# 行求和
print df2.sum(1)
a 6
b 15
dtype: int64
# 对每个元素乘以2
print df2.apply(lambda x:x*2)
col1 col2 col3
a 2 4 6
b 8 10 12
# 对每个元素求平方(支持ndarray一样的向量化操作)
print df2**2
col1 col2 col3
a 1 4 9
b 16 25 36
列扩充
# 对DataFrame对象进行列扩充
df2['col4'] = ['cnn','rnn']
print df2
col1 col2 col3 col4
a 1 2 3 cnn
b 4 5 6 rnn
# 也可以通过一个新的DataFrame对象来定义一个新列,索引自动对应
df2['col5'] = pd.DataFrame(['MachineLearning','DeepLearning'],index=['a','b'])
print df2
col1 col2 col3 col4 col5
a 1 2 3 cnn MachineLearning
b 4 5 6 rnn DeepLearning
行扩充
# 行进行扩充
print df2.append(pd.DataFrame({'col1':7,'col2':8,'col3':9,'col4':'rcnn','col5':'ReinforcementLearning'},index=['c']))
col1 col2 col3 col4 col5
a 1 2 3 cnn MachineLearning
b 4 5 6 rnn DeepLearning
c 7 8 9 rcnn ReinforcementLearning
注意!
# 如果在进行 行扩充时候没有,指定index的参数,索引会被数字取代
print df2.append({'col1':10,'col2':11,'col3':12,'col4':'frnn','col5':'DRL'},ignore_index=True)
col1 col2 col3 col4 col5
0 1 2 3 cnn MachineLearning
1 4 5 6 rnn DeepLearning
2 10 11 12 frnn DRL
# 以上的行扩充,并没有真正修改,df2这个DataFrame对象,除非
df2 = df2.append(pd.DataFrame({'col1':7,'col2':8,'col3':9,'col4':'rcnn','col5':'ReinforcementLearning'},index=['c']))
print df2
col1 col2 col3 col4 col5
a 1 2 3 cnn MachineLearning
b 4 5 6 rnn DeepLearning
c 7 8 9 rcnn ReinforcementLearning
c 7 8 9 rcnn ReinforcementLearning
print df2.loc['c']
col1 col2 col3 col4 col5
c 7 8 9 rcnn ReinforcementLearning
c 7 8 9 rcnn ReinforcementLearning
DataFrame对象的合并
# DataFrame 对象的合并
df_a = pd.DataFrame(['wang','jing','hui','is','a','master'],columns=['col6'],index=['a','b','c','d','e','f'])
print df_a
col6
a wang
b jing
c hui
d is
e a
f master
# 默认合并,只保留dfb中的全部索引
dfb = pd.DataFrame([1,2,4,5,6,7],columns=['col1'],index=['a','b','c','d','f','g'])
print dfb.join(df_a)
col1 col6
a 1 wang
b 2 jing
c 4 hui
d 5 is
f 6 master
g 7 NaN
# 默认合并之接受索引已经存在的值
# 通过指定参数 how,指定合并的方式
print dfb.join(df_a,how='inner') # 合并两个DataFrame对象的交集
col1 col6
a 1 wang
b 2 jing
c 4 hui
d 5 is
f 6 master
# 合并两个DataFrame对象的并集
print dfb.join(df_a,how='outer')
col1 col6
a 1.0 wang
b 2.0 jing
c 4.0 hui
d 5.0 is
e NaN a
f 6.0 master
g 7.0 NaN
import pandas as pd
import numpy as np
#可以是一组列表
df1 = pd.DataFrame([1,2,3,4,5],columns = ['cols'],index = ['a','b','c','d','e'])
print(df1)
#可以是多组列表
df2 = pd.DataFrame([[1,2,3],[4,5,6]],columns=['col1','col2','col3'],index=['a','b'])
print(df2)
#可以是数组
df3 = pd.DataFrame(np.array([[1,2],[3,4]]),columns=['col1','col2'],index = ['a','b'])
print(df3)
#可以是字典
df4 = pd.DataFrame({'col1':[1,3],'col2':[2,4]},index = ['a','b'])
print(df4)
#DataFrame对象的基本操作
print(df2.index) #查询横向标签及数据结构
print(df2.columns) #查询纵向标签及数据结构
print(df2.loc['b']) #根据横向标签(索引名)查询数据
print(df2.iloc[1]) #根据(数字索引)查询数据
print(df2.loc[['a','b']]) #根据多个索引名查询多行数据
print (df1.loc[df1.index[1:3]]) #根据数字索引,从df1[1]开始到第3个元素结束
print(df2[['col1','col3']])#直接访问列数据
#计算
#DataFrame元素求和
#默认是每列元素的求和
print(df2.sum()) #列求和,每一列元素相加
print(df2.sum(1)) #行求和,第二行元素相加
print(df2.apply(lambda x:x*2)) #用lambda函数对df2中的所有值进行乘以二
print(df2*2) #也可以直接执行乘以二,结果相同
print(df2**2)#其他运算符也可以
#列扩充
df2['col4'] = ['cnn','rnn']
print(df2)
#也可以通过一个新的DataFrame对象
df2['col5'] = pd.DataFrame(['MachineLearing','DeepLearning'],index=['a','b'])
print(df2)
#行扩充
print (df2.append(pd.DataFrame({'col1':7,'col2':8,'col3':9,'col4':10,'col5':5},index=['c'])))
#注意 !
#如果在进行行扩充的时候没有指定index的参数,索引会被数字取代
#由于以上并没有对df2修改进行保存,所以并没有真正修改,以下被修改2次
df2=df2.append(pd.DataFrame({'col1':11,'col2':12,'col3':13,'col4':14,'col5':15},index=['d']))
df2=df2.append(pd.DataFrame({'col1':11,'col2':12,'col3':13,'col4':14,'col5':15},index=['d']))
print(df2)
print(df2.loc['d']) #按索引搜寻数据
#DataFrame 对象的合并
index_list=['a','b','c','d','e','f']
df_a = pd.DataFrame(['a','b','c','d','e','f'],columns=['col6'],index=index_list)
print(df_a)
#默认合并,只保留df1的全部索引,会丢弃df_a的多余内容
print(df1.join(df_a))
#默认合并只接受df1索引已经存在的值
#通过制定参数how,制定合并方式
print(df1.join(df_a,how='inner')) #合并两个DataFrame的交集,删除重合区域
print(df1.join(df_a,how='outer')) #合并两个DataFrame的并集,自动补充NONE