Python Pandas 串联


Pandas 提供了各种方便组合的工具 系列,数据框 , and Panel objects.

 pd.concat(objs,axis=0,join='outer',join_axes=None,
ignore_index=False)
  • objs : 这是Series、DataFrame或Panel对象的序列或映射。

  • axis : {0, 1, ...},默认0。这是要连接的轴。

  • join : {‘inner’, ‘outer’},默认‘outer’。如何处理其他轴上的索引。外部用于联合,内部用于交叉。

  • 忽略索引 :布尔型,默认为False。如果为 True,则不要使用连接轴上的索引值。结果轴将标记为 0, ..., n - 1。

  • 连接轴 : 这是Index对象的列表。用于其他 (n-1) 轴的特定索引,而不是执行内部/外部集逻辑。

连接对象


The concat 函数完成了沿轴执行连接操作的所有繁重工作。让我们创建不同的对象并进行连接。

import pandas as pd

one = pd.DataFrame({
    'Name': ['Alex', 'Amy', 'Allen', 'Alice', 'Ayoung'],
    'subject_id':['sub1','sub2','sub4','sub6','sub5'],
    'Marks_scored':[98,90,87,69,78]},
    index=[1,2,3,4,5])

two = pd.DataFrame({
    'Name': ['Billy', 'Brian', 'Bran', 'Bryce', 'Betty'],
    'subject_id':['sub2','sub4','sub3','sub6','sub5'],
    'Marks_scored':[89,80,79,97,88]},
    index=[1,2,3,4,5])
print pd.concat([one,two])

Its output 如下:

    Marks_scored     Name   subject_id
1             98     Alex         sub1
2             90      Amy         sub2
3             87    Allen         sub4
4             69    Alice         sub6
5             78   Ayoung         sub5
1             89    Billy         sub2
2             80    Brian         sub4
3             79     Bran         sub3
4             97    Bryce         sub6
5             88    Betty         sub5

假设我们想要将特定的键与切碎的 DataFrame 的每个片段相关联。我们可以通过使用 keys 争论:

import pandas as pd

one = pd.DataFrame({
    'Name': ['Alex', 'Amy', 'Allen', 'Alice', 'Ayoung'],
    'subject_id':['sub1','sub2','sub4','sub6','sub5'],
    'Marks_scored':[98,90,87,69,78]},
    index=[1,2,3,4,5])

two = pd.DataFrame({
    'Name': ['Billy', 'Brian', 'Bran', 'Bryce', 'Betty'],
    'subject_id':['sub2','sub4','sub3','sub6','sub5'],
    'Marks_scored':[89,80,79,97,88]},
    index=[1,2,3,4,5])
print pd.concat([one,two],keys=['x','y'])

Its output 如下:

x  1  98    Alex    sub1
    2  90    Amy     sub2
    3  87    Allen   sub4
    4  69    Alice   sub6
    5  78    Ayoung  sub5
y  1  89    Billy   sub2
    2  80    Brian   sub4
    3  79    Bran    sub3
    4  97    Bryce   sub6
    5  88    Betty   sub5

结果的索引被复制;每个索引都重复。

如果结果对象必须遵循自己的索引,则设置 忽略索引 to True .

import pandas as pd

one = pd.DataFrame({
    'Name': ['Alex', 'Amy', 'Allen', 'Alice', 'Ayoung'],
    'subject_id':['sub1','sub2','sub4','sub6','sub5'],
    'Marks_scored':[98,90,87,69,78]},
    index=[1,2,3,4,5])

two = pd.DataFrame({
    'Name': ['Billy', 'Brian', 'Bran', 'Bryce', 'Betty'],
    'subject_id':['sub2','sub4','sub3','sub6','sub5'],
    'Marks_scored':[89,80,79,97,88]},
    index=[1,2,3,4,5])
print pd.concat([one,two],keys=['x','y'],ignore_index=True)

Its output 如下:

    Marks_scored     Name    subject_id
0             98     Alex          sub1
1             90      Amy          sub2
2             87    Allen          sub4
3             69    Alice          sub6
4             78   Ayoung          sub5
5             89    Billy          sub2
6             80    Brian          sub4
7             79     Bran          sub3
8             97    Bryce          sub6
9             88    Betty          sub5

观察,索引完全改变并且键也被覆盖。

如果需要同时添加两个对象 axis=1 ,然后将追加新列。

import pandas as pd

one = pd.DataFrame({
    'Name': ['Alex', 'Amy', 'Allen', 'Alice', 'Ayoung'],
    'subject_id':['sub1','sub2','sub4','sub6','sub5'],
    'Marks_scored':[98,90,87,69,78]},
    index=[1,2,3,4,5])

two = pd.DataFrame({
    'Name': ['Billy', 'Brian', 'Bran', 'Bryce', 'Betty'],
    'subject_id':['sub2','sub4','sub3','sub6','sub5'],
    'Marks_scored':[89,80,79,97,88]},
    index=[1,2,3,4,5])
print pd.concat([one,two],axis=1)

Its output 如下:

    Marks_scored    Name  subject_id   Marks_scored    Name   subject_id
1           98      Alex      sub1         89         Billy         sub2
2           90       Amy      sub2         80         Brian         sub4
3           87     Allen      sub4         79          Bran         sub3
4           69     Alice      sub6         97         Bryce         sub6
5           78    Ayoung      sub5         88         Betty         sub5

使用追加连接

一个有用的 concat 快捷方式是 Series 和 DataFrame 上的 append 实例方法。这些方法实际上早于 concat。他们串联在一起 axis=0 ,即指标:

import pandas as pd

one = pd.DataFrame({
    'Name': ['Alex', 'Amy', 'Allen', 'Alice', 'Ayoung'],
    'subject_id':['sub1','sub2','sub4','sub6','sub5'],
    'Marks_scored':[98,90,87,69,78]},
    index=[1,2,3,4,5])

two = pd.DataFrame({
    'Name': ['Billy', 'Brian', 'Bran', 'Bryce', 'Betty'],
    'subject_id':['sub2','sub4','sub3','sub6','sub5'],
    'Marks_scored':[89,80,79,97,88]},
    index=[1,2,3,4,5])
print one.append(two)

Its output 如下:

    Marks_scored    Name  subject_id
1           98      Alex      sub1
2           90       Amy      sub2
3           87     Allen      sub4
4           69     Alice      sub6
5           78    Ayoung      sub5
1           89     Billy      sub2
2           80     Brian      sub4
3           79      Bran      sub3
4           97     Bryce      sub6
5           88     Betty      sub5

The append 函数也可以接受多个对象:

import pandas as pd

one = pd.DataFrame({
    'Name': ['Alex', 'Amy', 'Allen', 'Alice', 'Ayoung'],
    'subject_id':['sub1','sub2','sub4','sub6','sub5'],
    'Marks_scored':[98,90,87,69,78]},
    index=[1,2,3,4,5])

two = pd.DataFrame({
    'Name': ['Billy', 'Brian', 'Bran', 'Bryce', 'Betty'],
    'subject_id':['sub2','sub4','sub3','sub6','sub5'],
    'Marks_scored':[89,80,79,97,88]},
    index=[1,2,3,4,5])
print one.append([two,one,two])

Its output 如下:

    Marks_scored   Name    subject_id
1           98     Alex          sub1
2           90      Amy          sub2
3           87    Allen          sub4
4           69    Alice          sub6
5           78   Ayoung          sub5
1           89    Billy          sub2
2           80    Brian          sub4
3           79     Bran          sub3
4           97    Bryce          sub6
5           88    Betty          sub5
1           98     Alex          sub1
2           90      Amy          sub2
3           87    Allen          sub4
4           69    Alice          sub6
5           78   Ayoung          sub5
1           89    Billy          sub2
2           80    Brian          sub4
3           79     Bran          sub3
4           97    Bryce          sub6
5           88    Betty          sub5

时间序列


Pandas 提供了一个强大的工具来处理时间序列数据,尤其是在金融领域。在处理时间序列数据时,我们经常会遇到以下情况:

  • 生成时间序列
  • 将时间序列转换为不同的频率

Pandas 提供了一套相对紧凑且独立的工具来执行上述任务。

获取当前时间

日期时间.now() 为你提供当前日期和时间。

import pandas as pd

print pd.datetime.now()

Its output 如下:

2017-05-11 06连接对象13.393147

创建时间戳

时间戳数据是将值与时间点相关联的最基本类型的时间序列数据。对于 pandas 对象,这意味着使用时间点。举个例子:

import pandas as pd

print pd.Timestamp('2017-03-01')

Its output 如下:

2017-03-01 00:00:00

也可以转换整数或浮点纪元时间。这些的默认单位是纳秒(因为这些是时间戳的存储方式)。但是,通常时期存储在另一个可以指定的单元中。让我们再举一个例子

import pandas as pd

print pd.Timestamp(1587687255,unit='s')

Its output 如下:

2020-04-24 00争论:15

创建时间范围

import pandas as pd

print pd.date_range("11:00", "13:30", freq="30min").time

Its output 如下:

[datetime.time(11, 0) datetime.time(11, 30) datetime.time(12, 0)
datetime.time(12, 30) datetime.time(13, 0) datetime.time(13, 30)]

改变时间的频率

import pandas as pd

print pd.date_range("11:00", "13:30", freq="H").time

Its output 如下:

[datetime.time(11, 0) datetime.time(12, 0) datetime.time(13, 0)]

转换为时间戳

要转换类日期对象的系列或列表类对象,例如字符串、纪元或混合,你可以使用 to_datetime 功能。传递时,这将返回一个系列(具有相同的索引),而一个 列表式 被转换为 DatetimeIndex .看看下面的例子:

import pandas as pd

print pd.to_datetime(pd.Series(['Jul 31, 2009','2010-01-10', None]))

Its output 如下:

0  2009-07-31
1  2010-01-10
2         NaT
dtype: datetime64[ns]

NaT means 不是时间 (相当于 NaN)

让我们再举一个例子。

import pandas as pd

print pd.to_datetime(['2005/11/23', '2010.12.31', None])

Its output 如下:

DatetimeIndex(['2005-11-23', '2010-12-31', 'NaT'], dtype='datetime64[ns]', freq=None)