DataFrame对象的创建,修改,合并
import pandas as pd
import numpy as np
- 1
- 2
- 3
创建DataFrame对象
# 创建DataFrame对象
df = pd.DataFrame([1, 2, 3, 4, 5], columns=['cols'], index=['a','b','c','d','e'])
print df
- 1
- 2
- 3
cols
a 1
b 2
c 3
d 4
e 5
- 1
- 2
- 3
- 4
- 5
- 6
- 7
df2 = pd.DataFrame([[1, 2, 3],[4, 5, 6]], columns=['col1','col2','col3'], index=['a','b'])
print df2
- 1
- 2
col1 col2 col3
a 1 2 3
b 4 5 6
- 1
- 2
- 3
- 4
df3 = pd.DataFrame(np.array([[1,2],[3,4]]), columns=['col1','col2'], index=['a','b'])
print df3
- 1
- 2
col1 col2
a 1 2
b 3 4
- 1
- 2
- 3
- 4
df4 = pd.DataFrame({'col1':[1,3],'col2':[2,4]},index=['a','b'])
print df4
- 1
- 2
col1 col2
a 1 2
b 3 4
- 1
- 2
- 3
- 4
创建DataFrame对象的数据可以为列表,数组和字典,列名和索引为列表对象
- 1
基本操作
# DataFrame对象的基本操作
df2.index
- 1
- 2
Index([u'a', u'b'], dtype='object')
- 1
- 2
df2.columns
- 1
Index([u'col1', u'col2', u'col3'], dtype='object')
- 1
- 2
# 根据索引查看数据
df2.loc['a']
# 索引为a这一行的数据
# df2.iloc[0] 跟上面的操作等价,一个是根据索引名,一个是根据数字索引访问数据
- 1
- 2
- 3
- 4
col1 1
col2 2
col3 3
Name: a, dtype: int64
- 1
- 2
- 3
- 4
- 5
print df2.loc[['a','b']] # 访问多行数据,索引参数为一个列表对象
- 1
col1 col2 col3
a 1 2 3
b 4 5 6
- 1
- 2
- 3
- 4
print df.loc[df.index[1:3]]
- 1
cols
b 2
c 3
- 1
- 2
- 3
- 4
# 访问列数据
print df2[['col1','col3']]
- 1
- 2
col1 col3
a 1 3
b 4 6
- 1
- 2
- 3
- 4
计算
# DataFrame元素求和
# 默认是对每列元素求和
print df2.sum()
- 1
- 2
- 3
col1 5
col2 7
col3 9
dtype: int64
- 1
- 2
- 3
- 4
- 5
# 行求和
print df2.sum(1)
- 1
- 2
a 6
b 15
dtype: int64
- 1
- 2
- 3
- 4
# 对每个元素乘以2
print df2.apply(lambda x:x*2)
- 1
- 2
col1 col2 col3
a 2 4 6
b 8 10 12
- 1
- 2
- 3
- 4
# 对每个元素求平方(支持ndarray一样的向量化操作)
print df2**2
- 1
- 2
col1 col2 col3
a 1 4 9
b 16 25 36
- 1
- 2
- 3
- 4
列扩充
# 对DataFrame对象进行列扩充
df2['col4'] = ['cnn','rnn']
print df2
- 1
- 2
- 3
col1 col2 col3 col4
a 1 2 3 cnn
b 4 5 6 rnn
- 1
- 2
- 3
- 4
# 也可以通过一个新的DataFrame对象来定义一个新列,索引自动对应
df2['col5'] = pd.DataFrame(['MachineLearning','DeepLearning'],index=['a','b'])
print df2
- 1
- 2
- 3
col1 col2 col3 col4 col5
a 1 2 3 cnn MachineLearning
b 4 5 6 rnn DeepLearning
- 1
- 2
- 3
- 4
行扩充
# 行进行扩充
print df2.append(pd.DataFrame({'col1':7,'col2':8,'col3':9,'col4':'rcnn','col5':'ReinforcementLearning'},index=['c']))
- 1
- 2
col1 col2 col3 col4 col5
a 1 2 3 cnn MachineLearning
b 4 5 6 rnn DeepLearning
c 7 8 9 rcnn ReinforcementLearning
- 1
- 2
- 3
- 4
- 5
注意!
# 如果在进行 行扩充时候没有,指定index的参数,索引会被数字取代
print df2.append({'col1':10,'col2':11,'col3':12,'col4':'frnn','col5':'DRL'},ignore_index=True)
- 1
- 2
col1 col2 col3 col4 col5
0 1 2 3 cnn MachineLearning
1 4 5 6 rnn DeepLearning
2 10 11 12 frnn DRL
- 1
- 2
- 3
- 4
- 5
# 以上的行扩充,并没有真正修改,df2这个DataFrame对象,除非
df2 = df2.append(pd.DataFrame({'col1':7,'col2':8,'col3':9,'col4':'rcnn','col5':'ReinforcementLearning'},index=['c']))
print df2
- 1
- 2
- 3
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
- 1
- 2
- 3
- 4
- 5
- 6
print df2.loc['c']
- 1
col1 col2 col3 col4 col5
c 7 8 9 rcnn ReinforcementLearning
c 7 8 9 rcnn ReinforcementLearning
- 1
- 2
- 3
- 4
DataFrame对象的合并
# DataFrame 对象的合并
df_a = pd.DataFrame(['wang','jing','hui','is','a','master'],columns=['col6'],index=['a','b','c','d','e','f'])
print df_a
- 1
- 2
- 3
col6
a wang
b jing
c hui
d is
e a
f master
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
# 默认合并,只保留dfb中的全部索引
dfb = pd.DataFrame([1,2,4,5,6,7],columns=['col1'],index=['a','b','c','d','f','g'])
print dfb.join(df_a)
- 1
- 2
- 3
col1 col6
a 1 wang
b 2 jing
c 4 hui
d 5 is
f 6 master
g 7 NaN
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
# 默认合并之接受索引已经存在的值
# 通过指定参数 how,指定合并的方式
print dfb.join(df_a,how='inner') # 合并两个DataFrame对象的交集
- 1
- 2
- 3
col1 col6
a 1 wang
b 2 jing
c 4 hui
d 5 is
f 6 master
- 1
- 2
- 3
- 4
- 5
- 6
- 7
# 合并两个DataFrame对象的并集
print dfb.join(df_a,how='outer')
- 1
- 2
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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
<link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/markdown_views-ea0013b516.css">
</div>
感谢 https://blog.csdn.net/u014281392/article/details/75331570