从数据表中获取linspace列表

时间:2017-04-24 22:08:12

标签: python dataframe apply

我在python中有以下代码。这会产生漂亮的DataFrame:

df=pd.read_csv('spt.csv')

df1 = df[['TRACK_ID','POSITION_X','POSITION_Y','POSITION_T']].copy()

N = df1.groupby('TRACK_ID').size()           
max_time = (df1.groupby(['TRACK_ID'],sort=False)['POSITION_T'].size())*(0.062)
frames = max_time/N



data = pd.DataFrame({'N':N,'max_time':max_time,'frames':frames})
print(data)

               N  frames  max_time
TRACK_ID                      
0         50   0.062     3.100
1         42   0.062     2.604
2         49   0.062     3.038
3          4   0.062     0.248
4         21   0.062     1.302
5         15   0.062     0.930
6         11   0.062     0.682
7          4   0.062     0.248
8         10   0.062     0.620
9          7   0.062     0.434

然后我尝试使用我的dataFrame参数生成一个linspace:

t=data.apply(np.linspace(0,max_time,N), axis=1)

这会产生错误:

"{0}".format(str(converter)))

TypeError: cannot convert the series to <class 'int'>

我已经更改为浮点数并且没有任何更改,请有人可以使用DataFrame的参数解决生成linspace列表的正确方法吗?

1 个答案:

答案 0 :(得分:0)

这就是你追求的吗? (根据评论更新,从0.062开始分类)

data.apply(lambda x: tuple(np.linspace(0.062,x.max_time,x.N)),axis=1)
Out[722]: 
0    (0.062, 0.124, 0.186, 0.248, 0.31, 0.372, 0.43...
1    (0.062, 0.124, 0.186, 0.248, 0.31, 0.372, 0.43...
2    (0.062, 0.124, 0.186, 0.248, 0.31, 0.372, 0.43...
3                         (0.062, 0.124, 0.186, 0.248)
4    (0.062, 0.124, 0.186, 0.248, 0.31, 0.372, 0.43...
5    (0.062, 0.124, 0.186, 0.248, 0.31, 0.372, 0.43...
6    (0.062, 0.124, 0.186, 0.248, 0.31, 0.372, 0.43...
7                         (0.062, 0.124, 0.186, 0.248)
8    (0.062, 0.124, 0.186, 0.248, 0.31, 0.372, 0.43...
9     (0.062, 0.124, 0.186, 0.248, 0.31, 0.372, 0.434)
dtype: object