【问题标题】:How to handle Series and Array with pandas and numpy together?如何使用 pandas 和 numpy 一起处理 Series 和 Array?
【发布时间】:2016-05-10 22:18:33
【问题描述】:

我是 Python 新手,我对所有这些数据类型(例如 Series、Array、List 等)感到非常困惑。这可能是一个非常开放的问题。我希望在使用 python 编码进行数据分析时对一般实践有所了解。

大量阅读表明 numpy 和 pandas 是我进行数据分析所需的两个模块。但是,我觉得这很难而且很奇怪,因为它们以两种不同的数据类型(即系列和数组)操作/生成数据。在进行任何类型的数据操作之前,需要将其中一种数据类型转换为另一种数据类型是否正常/自然?想知道你会做什么吗?非常感谢。

例如:

 import pandas as pd
 import numpy as np

 # create some data
 df = pd.DataFrame(np.random.randn(10, 3), columns=['a', 'b', 'c'])
 x = np.random.randn(10, 1)

 # data manipulation
 A = df['a']

 # Question 1:
 # If I want to perform a element by element addition between x and A
 # How should I do?  Simple x + A doesn't work but it seems strange to 
 # me that if I have to convert the data type everytime 

 # Question 2:
 # I'd like to combine to two columns together
 # concatenate or hstack both don't work

【问题讨论】:

  • 你想得到什么numpy.arrayspd.Seriespd.Dataframes
  • 我想我会在最后想要数据框,因为我从数据框开始(因为我使用熊猫导入数据)。基本上,我发现它们彼此不兼容(两个模块),这很烦人,我想知道我的方向是否正确(几乎每个操作都需要额外的步骤/功能)。

标签: python arrays numpy pandas series


【解决方案1】:

另外,您的arrays/Series 应该具有相同的尺寸:

In [98]: A.shape
Out[98]: (10,)

In [99]: x.shape
Out[99]: (10, 1)

您可以使用 reshape(-1) 将向量转换为数组:

In [100]: x.reshape(-1).shape
Out[100]: (10,)

然后你可以用pd.Series A:

In [61]: A + x.reshape(-1)
Out[61]:
0   -1.186957
1   -0.165563
2    0.882490
3    4.544357
4    2.698414
5    0.396110
6   -0.199209
7    3.282942
8    2.448213
9   -0.543727
Name: a, dtype: float64

对于您的第二个问题,您需要为向量重塑您的 A Series。你可以用reshape

In [97]: np.hstack([A.values.reshape(A.size,1), x])
Out[97]:
array([[ 0.3158111 , -1.50276813],
       [-1.09532212,  0.92975954],
       [-0.77048623,  1.65297592],
       [ 2.14690242,  2.39745455],
       [ 1.63367806,  1.06473634],
       [ 0.09134512,  0.3047644 ],
       [ 0.02019805, -0.21940726],
       [ 0.87008192,  2.41286007],
       [ 1.25315724,  1.19505578],
       [-0.60156045,  0.05783343]])

如果你想获得pd.DataFrame,你可以使用pd.concat

In [108]: pd.concat([A, pd.Series(x.reshape(-1))], axis=1)
Out[108]:
          a         0
0  0.315811 -1.502768
1 -1.095322  0.929760
2 -0.770486  1.652976
3  2.146902  2.397455
4  1.633678  1.064736
5  0.091345  0.304764
6  0.020198 -0.219407
7  0.870082  2.412860
8  1.253157  1.195056
9 -0.601560  0.057833

编辑

来自docsreshape(-1)

newshape : 整数或整数元组
新形状应与原始形状兼容。如果是整数,则结果将是该长度的一维数组。一个形状维度可以是-1。在这种情况下,该值是从数组的长度和剩余维度推断出来的。

【讨论】:

  • .reshape(-1) 是什么意思?谢谢
  • 已编辑答案
  • @Lafayette 请注意,reshape(-1) 适用于原始数组的任何形状,而 reshape(10) 仅适用于大小为 10 的向量。
【解决方案2】:

需要转换任一数据是否正常/自然 在进行任何类型的数据操作之前键入另一个?

有时需要,有时不需要。如有疑问,请执行。

也就是说,记住 Python 之禅:

  • 显式优于隐式。
  • 面对模棱两可,拒绝猜测的诱惑。

即使某些 API 会尽力为您转换类型(numpy 和 pandas 在这方面非常出色),显式类型转换也可以使您的代码更具可读性和更易于调试。

问题 1: 如果我想在 x 和 A 之间逐个元素相加,我应该怎么做?简单的 x + A 不起作用,但我觉得很奇怪,如果我每次都必须转换数据类型

在这种情况下,您不必转换数据类型,但您需要兼容的形状。

>>> print(A.shape)
(10,)
>>> print(x.shape)
(10, 1)
>>> print(A + x.reshape(10))
0   -0.207131
1   -2.117012
2    0.925545
3   -2.187705
4    1.226458
5    2.144904
6   -0.956781
7    1.956246
8    0.060132
9    1.332417
Name: a, dtype: float64

问题 2:我想将两列组合在一起 concatenate 或 hstack 都不起作用

目前尚不清楚所需的输出是什么,但我认为这又是形状问题,而不是类型问题。这是熊猫方式的一个选项:

>>> print(pd.concat([A, pd.Series(x.reshape(10))], axis=1))
          a         0
0 -0.158667 -0.048463
1 -0.847246 -1.269765
2 -0.128232  1.053778
3 -1.316113 -0.871593
4  1.057044  0.169414
5  3.188343 -1.043439
6 -0.032524 -0.924257
7  1.412443  0.543803
8 -0.730386  0.790519
9  0.289796  1.042621

【讨论】:

    猜你喜欢
    • 2017-04-01
    • 2020-01-22
    • 2020-12-10
    • 2015-04-01
    • 2021-07-03
    • 2020-01-08
    • 2017-05-04
    • 2015-12-10
    • 2020-11-09
    相关资源
    最近更新 更多