针对array,DataFrame和lit的添加合并等操作

np.array合并

arr1 = np.arange(1,10)
arr2 = np.arange(20,30)
arr3 = np.array([[1,2],[2,3]])
arr4 = np.array([[1,2,3],[4,5,6]])

# 合并方法一:concatenate( *arr, axis=0 )
arr_new = np.concatenate( (arr1, arr2) )
>>>
	array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29])

arr_new = np.concatenate( (arr1, arr2) ,0)
>>>
	array([[1, 2, 1, 2, 3],
       [2, 3, 4, 5, 6]])

# 合并方法二:append(value1, value2, axis=None)
arr_new = np.append( arr3, arr4 )
>>>
	array([1, 2, 2, 3, 1, 2, 3, 4, 5, 6])

arr_new = np.append( arr3, arr4, 0 )
>>>
	array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 20, 21, 22, 23, 24, 25, 26, 27,
      28, 29])

arr_new = np.append( arr3, arr4, 1)
>>>
	array([[1, 2, 1, 2, 3],
       [2, 3, 4, 5, 6]])

list合并

ls1 = [1,2,3]
ls2 = [3,4,5]
ls3 = [[1,2],[3,4]]
ls4 = [[6,7,8],[3,5,6]]

# list直接合并
ls_new = ls1+ls2
>>>[1, 2, 3, 3, 4, 5]

ls_new = ls3 + ls4
>>>[[1, 2], [3, 4], [6, 7, 8], [3, 5, 6]]

# 利用zip进行合并
ls_new = list(zip(ls1,ls2))
>>>[(1, 3), (2, 4), (3, 5)]

# 利用numpy合并成数组
ls_new = np.hstack( (ls1,ls2) )
>>>
	array([1, 2, 3, 3, 4, 5])
ls_new = np.vstack( (ls3,ls4) )
>>>
	array([[1, 2, 6, 7, 8],
       [3, 4, 3, 5, 6]])

np.r_[ls1,ls2]
>>>
	array([1, 2, 3, 3, 4, 5])
np.c_[ls1,ls2]
>>>
	array([[1, 3],
       [2, 4],
       [3, 5]])

DataFrame添加行

from pandas import DataFrame
d = DataFrame([1,2,3])
>>>
		0
	-----
	0	1
	1	2
	2	3
# 添加一行
d.loc[len(d)] = -1
>>>
		0
	------
	0	1
	1	2
	2	3
	3	-1
# 添加一列
d[len(d)] = -2
>>>
		0	4
	------------
	0	1	-2
	1	2	-2
	2	3	-2
	3	-1	-2
DataFrame条件筛选/去重
# 采样两种类型
first_types = df_types.iloc[random.sample(range(len(df_types)),2)]
# 选择目标类型
first_datas = train_data[train_data['FIRST'].isin(first_types)]
# 去重
unique_types = first_types.drop_duplicates()
展开阅读全文

没有更多推荐了,返回首页