Creating array from a dataframe taking one column value as a reference
94 观看
1回复
95 作者的声誉
I am dealing with this issue since some days ago, but I couldn´t find an answer. Hope you can help me.
This is my dataframe:
Date Attribute Quantity
0 2017-12-14 large -39
0 2017-12-15 large -80
1 2017-12-15 large -30
2 2017-12-14 short -15
2 2017-12-15 short -100
4 2017-12-15 short -10
1 2017-12-15 short 20
3 2017-12-15 short 60
3 2017-12-15 big 80
5 2017-12-15 big 104
What I want to do? I would like to calculate XIRR for each Attribute
ítem. For this I require Date
and Quantity
(as an array), but based on each Attribute
item listed in the second column. For example, given large
, I would like to extract Dates
and quantities
(as an array) for large
.
Given that, I think my best choice is to create specific arrays based on the Attibute
column and then execute the aforementioned function (please, let me know if you consider another approach to this problem). So, I generated one array df1= df[['Date','Quantity']].as_matrix()
which produce
[[Timestamp('2017-12-14 00:00:00') -39]
[Timestamp('2017-12-15 00:00:00') -80]
[Timestamp('2017-12-15 00:00:00') -30]
[Timestamp('2017-12-14 00:00:00') -15]
[Timestamp('2017-12-15 00:00:00') -100]
[Timestamp('2017-12-15 00:00:00') -10]
[Timestamp('2017-12-15 00:00:00') -20]
[Timestamp('2017-12-15 00:00:00') 60]
[Timestamp('2017-12-15 00:00:00') -80]
[Timestamp('2017-12-15 00:00:00') 104]]
As you can see this array includes all the attributes, but I would like to get something like For / each
function according to each attibute in column Attribute
. How can I do this? Is this the best approach/alternative to my final goal?
Any help would be highly appreciated.
PD: I should mention that the function I would like to use works over attribute as a group (because it requieres dates and quantity, all together). It works like gruopby
.
Thanks
作者: Newbie 的来源 发布者: 2017 年 12 月 27 日回应 1
1像
1208 作者的声誉
Consider applying a function to each row of the DataFrame:
def row_func(row):
if row['Atribute'] == 'large':
return row['quantity']
etc...
df['new_column'] = df.apply(row_func, axis=1)
作者: it's-yer-boy-chet
发布者: 2017 年 12 月 27 日
来自类别的问题 :
- python 如何使用Python的itertools.groupby()?
- python Python:我在运行什么操作系统?
- python 如何使用Python创建可直接执行的跨平台GUI应用程序?
- python Python声音(“钟声”)
- python 使用Python创建加密的ZIP文件
- arrays 如何从C#数组中删除重复项?
- arrays 如何在C中确定数组的大小?
- arrays 在Ruby中将数组转换为散列的最佳方法是什么?
- arrays Comparing two byte arrays in .NET
- arrays 可以在MATLAB中完成并行遍历,就像在Python中一样吗?
- for-loop ++ i和i ++有什么区别?
- for-loop 关于动态的Haxe迭代
- for-loop 如何遍历Bash中变量定义的数字范围?
- for-loop C语言中完整的“ for”循环语法是什么?
- dataframe 如何访问向量中的最后一个值?
- dataframe 用于访问列表或数据框元素的方括号[]和双括号[[]]之间的区别
- dataframe 子集数据帧中的丢包因子级别
- dataframe 如何按多列对数据框进行排序
- dataframe 按逻辑条件过滤data.frame行