Python按⾏列合并列表、字典、数组Python 合并list,dict,数组
1.⽤字典将相同index的列放到同⼀列
# 将index相同的⾏放到同⼀列
from collections import defaultdict
data =[['a',1],['a',3],['b',1],['b',2],['c',1],['c',5]]
dict_data = defaultdict(set)
for line in data:
if len(line)>1:
dict_data[line[0]].update(line[1:])
print(dict_data)
#输出
out: defaultdict(<class'set'>,{'a':{1,3},'b':{1,2},'c':{1,5}})
2.函数返回多个值只取其中⼀个
#定义函数
def test():
a =1
b =2
c =3
return a,b,c
#取test函数的b
b = test()[1]
print("b = ",b)
#结果
b =2
3.python按⾏、列合并
3.1 python合并列表list
a =[1,2,3,4,5]
b =['A','B','C','D']
print(a)
#输出
[1,2,3,4,5,'A','B','C','D']
2.append():向列表尾部追加⼀个新元素,新元素只占⼀个索引位
a =[1,2,3,4,5]
b =['A','B','C','D']
a.append(b)
print(b)
#输出
[1,2,3,4,5,['A','B','C','D']]
3.a+b:看上去和extend()⼀样,但是⽣成了⼀个新的列表存两个列表的和
a =[1,2,3,4,5]
b =['A','B','C','D']
c = a+b
print(c)
#输出
[1,2,3,4,5,'A','B','C','D']
4.a+=b:效果和extend()⼀样,也是在原有列表上增加
a =[1,2,3,4,5]
b =['A','B','C','D']
c = a+b
print(c)
#输出
[1,2,3,4,5,'A','B','C','D']
5.切⽚,a[0:0]
a =[1,2,3,4,5]
b =['A','B','C','D']
a[0:0]= b
print(a)
#输出
['A','B','C','D',1,2,3,4,5]
a[1:0]= b
#输出
print(a)
[1,'A','B','C','D',2,3,4,5]
3.2python合并字典dict
相同key的value会被覆盖掉
1.dict(a,**b):b会覆盖a中key相同的value,结果是所有不同key的value, python 2 不⽀持,python3.5及以上⽀持#适⽤于所有key值不同的dict
a ={'age':17,'name':'Mary','gender':'female'}
b ={'age':19,'name':'Joe','grade':90,'class':'Two'}
c =dict(a,**b)
print(c)
#输出
{'age':19,'name':'Joe','sex':'female','grade':90,'class':'Two'}
2.dict(a.items()+b.items()) ,python3不⽀持
#仅适⽤于python2
a ={'age':17,'name':'Mary','gender':'female'}
b ={'age':19,'name':'Joe','grade':90,'class':'Two'}
c =dict(a.items()+b.items())
print c
#输出,b覆盖a中key相同的value
{'grade':90,'gender':'female','age':19,'name':'Joe','class':'Two'}
3.update()
a.update(b)
print(a)
#或者,不改变a
c ={}
c.update(a)#或者 c = a.copy()
c.update(b)
python 定义数组print(c)
#输出
{'age':19,'name':'Joe','gender':'female','grade':90,'class':'Two'}
4.字典的常规处理⽅法
for k, v in a.items():
c[k]= v
for k, v in b.items():
c[k]= v
print(c)
#输出
{'age':19,'name':'Joe','gender':'female','grade':90,'class':'Two'} 3.3python合并数组,numpy库
3.3.1 数组横向合并,按⾏
1.np.vstack()
import numpy as np
a =[[1,2,3],[4,5,6]]#列表和数组类型都适⽤
b =[[7,8,9],[3,3,3]]
c = np.vstack((a,b))
print(c)
#输出,类型是数组型
array([[1,2,3],
[4,5,6],
[7,8,9],
[3,3,3]])
2.np.r_[ ]
c = np.r_[a,b]
print(c)
#输出,类型是数组型
array([[1,2,3],
[4,5,6],
[7,8,9],
[3,3,3]])
c = np.concatenate([a,b],axis =0)
print(c)
#输出,类型是数组型
array([[1,2,3],
[4,5,6],
[7,8,9],
[3,3,3]])
3.3.2数组纵向合并,按列
1.np.hstack()
c = np.hstack((a,b))
print(c)
#输出,类型是数组型
array([[1,2,3,7,8,9],
[4,5,6,3,3,3]])
2.np.c_[ ]
c = np.c_[a,b]
print(c)
#输出,类型是数组型
array([[1,2,3,7,8,9],
[4,5,6,3,3,3]])
c = np.concatenate([a,b],axis =1)
print(c)
#输出,类型是数组型
array([[1,2,3,7,8,9],
[4,5,6,3,3,3]])
3.4python合并数组,pandas库
3.4.1 merge函数
参数描述
left参与合并的左侧DataFrame
right参与合并的右侧DataFrame
how连接⽅式:inner,默认;outer,left,right
on连接的列名,和sql连接的on类似left_on左侧DF⽤作连接的键
right_on右侧DF⽤作连接的键
left_index将左侧的⾏索引⽤作其连接键
right_on将右侧的⾏索引⽤作其连接键sort根据连接键对合并后的数据进⾏排序,默认True; 类似sql的order by suffixes字符串值元组,⽤于追加重叠列名的末尾,重叠列名重命名
copy设置False,可以在某些特殊情况下避免将数据复制到结果数据结构中
import pandas as pd
import numpy as np
df1 = pd.DataFrame({'key':['d','d','a','c'],'data1':range(4)})
#out
data1 key
00 d
11 d
22 a
33 c
df2 = pd.DataFrame({'key':['a','b','c'],'data2':range(3)})
#out
data1 key
00 a
11 b
22 c
(1) 默认合并,key相同合并data
df3 = pd.merge(df1,df2)#等同于(df2)
#out
data1 key data2
02 a 0
13 c 2
(2)内连接,取交集
df3 = (df2,on ='key', how ='inner')
#out
data1 key data2
02 a 0
13 c 2
(3)外连接,取并集,并有NaN填充
df3 = (df2, on ='key', how ='outer')
#out
data1 key data2
00.0 d NaN
1 1.0 d NaN
2 2.0 a 0.0
3 3.0 c 2.0
4 NaN b 1.0
(4)左连接,左侧DataFrame取全部,右侧DataFrame取部分
df3 = (df2, on ='key', how ='left')
#out
data1 key data2
00 d NaN
11 d NaN
22 a 0.0
33 c 2.0
(5)右连接,右侧DataFrame取全部,左侧DataFrame取部分
df3 = (df2, on ='key', how ='right')
#out
data1 key data2
0 2.0 a 0
1 3.0 c 2
2 NaN b 1
(6)如果左右侧DataFrame的连接键列名不⼀致,但是取值有重叠,可使⽤left_on、right_on来指定左右连接键
df3 = pd.DataFrame({'lkey':['b','b','a','c','a','a','b'],'data1':range(7)})
df4 = pd.DataFrame({'rkey':['a','b','d'],'data2':range(3)})
<(df4,left_on ='lkey',right_on ='rkey',how ='inner')
#out
data1 lkey data2 rkey
00 b 1 b
11 b 1 b
26 b 1 b
32 a 0 a
44 a 0 a
55 a 0 a
3.4.2 索引合并
当连接键位于索引中时,成为索引上的合并,可以通过merge函数,传⼊left_index、right_index来说明应该被索引的情况
left1 = pd.DataFrame({'key':['a','b','a','a','b','c'],'value':range(6)})
right1 = pd.DataFrame({'group_val':[3.5,7]},index =['a','b'])
df = pd.merge(left1,right1,left_on ='key',right_index =True)
#out
key value group_val
0 a 0 3.5
2 a 2 3.5
3 a 3 3.5
1 b 17.0
4 b 47.0
3.4.3 concat轴向连接
参数描述
objs对象(列表或字典),对象的数据类型是pandas,不可为空
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论