在pandas数据帧上实现numpy arange实现

时间:2017-06-14 06:02:48

标签: python pandas numpy

我有一个数据框,如此,

import pandas as pd
import numpy as np


df = pd.DataFrame({'a': [0, 0.5, 0.2],
                   'b': [1,1,0.3]})
print (df)
     a    b
0  0.0  1.0
1  0.5  1.0
2  0.2  0.3

我想生成一个看起来像

的系列
pd.Series ([np.arange ( start = 0, stop = 1, step = 0.1), 
 np.arange ( start = 0.5, stop = 1, step = 0.1),
 np.arange ( start = 0.2, stop = 0.3, step = 0.1)])

0    [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, ...
1                            [0.5, 0.6, 0.7, 0.8, 0.9]
2                                                [0.2]
dtype: object

我试图用lambda函数执行此操作并收到错误,如此

foo = lambda x: np.arange(start = x.a, stop = x.b, step = 0.1)
print (df.apply(foo, axis =1))

ValueError: Shape of passed values is (3, 10), indices imply (3, 2)

我不确定这意味着什么。有没有更好/更正确的方法呢?

2 个答案:

答案 0 :(得分:3)

itertuplesSeries构造函数一起使用:

s = pd.Series([np.arange(x.a, x.b, .1) for x in df.itertuples()], index=df.index)
print (s)
0    [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, ...
1                            [0.5, 0.6, 0.7, 0.8, 0.9]
2                                                [0.2]
dtype: object
s = pd.Series([np.arange(x.a, x.b, .1) for i, x in df.iterrows()], index=df.index)
print (s)
0    [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, ...
1                            [0.5, 0.6, 0.7, 0.8, 0.9]
2                                                [0.2]
dtype: object

应用仅适用于转换为tuple

foo = lambda x: tuple(np.arange(start = x.a, stop = x.b, step = 0.1))
print (df.apply(foo, axis = 1))
0    (0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, ...
1                            (0.5, 0.6, 0.7, 0.8, 0.9)
2                                               (0.2,)
dtype: object

答案 1 :(得分:2)

我使用理解

pd.Series([np.arange(a, b, .1) for a, b in zip(df.a, df.b)], df.index)

0    [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, ...
1                            [0.5, 0.6, 0.7, 0.8, 0.9]
2                                                [0.2]
dtype: object