个性化阅读
专注于IT技术分析

Pandas如何串联数据?

NumPy的连接数据用于按行或列连接两个数组。它可以采用两个或更多个相同形状的数组, 并且按行串联作为默认类型, 即axis = 0。

范例1:

# import numpy
import numpy as np
arr1 = np.arange(9)
arr1
arr2d_1 = array.reshape((3, 3))
arr2d_1
	arr2d_1 = np.arange(10, 19).reshape(3, 3)
arr2d_1


# concatenate 2 numpy arrays: row-wise
np.concatenate((arr2d_1, arr2d_2))

输出

array([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [10, 11, 12], [13, 14, 15], [16, 17, 18]])

范例2:

import pandas as pd

one = pd.DataFrame({'Name': ['Parker', 'Phill', 'Smith'], 'id':[108, 119, 127]}, index=['A', 'B', 'C'])
two = pd.DataFrame({'Name': ['Terry', 'Jones', 'John'], 'id':[102, 125, 112]}, index=['A', 'B', 'C'])
print(pd.concat([one, two]))

输出

Name     id
A   Parker   108
B   Phill    119
C   Smith    127
A   Terry    102
B   Jones    125
C   John     112

范例3:

import pandas as pd
one = pd.DataFrame({'Name': ['Parker', 'Phill', 'Smith'], 'id':[108, 119, 127]}, index=['A', 'B', 'C'])
two = pd.DataFrame({'Name': ['Terry', 'Jones', 'John'], 'id':[102, 125, 112]}, index=['A', 'B', 'C'])
print(pd.concat([one, two], keys=['x', 'y']))

输出

Name   id
x A  Parker  108
B   Phill119
 C   Smith  127
y A   Terry  102
 B   Jones  125
C    John  112

赞(0)
未经允许不得转载:srcmini » Pandas如何串联数据?

评论 抢沙发

评论前必须登录!