我有两个数据框:
DF1
GOOG AAPL XOM IBM Cash
Date
2009-01-14 NaN 150 NaN NaN NaN
2009-01-21 NaN -150 NaN 400 NaN
DF2
GOOG AAPL XOM IBM Cash
Date
2009-01-14 NaN 100 NaN NaN NaN
2009-01-21 NaN 30 NaN 15 NaN
如何将它们相乘,以便创建一个新的DF,如下所示: 我基本上是在尝试DF1,看看DF2中是否存在索引和列,如果存在,请将它们相乘。
重要的是要注意DF1和DF2的索引可能不完全相同。因此,答案必须考虑到这一点。
GOOG AAPL XOM IBM Cash
Date
2009-01-14 NaN 15000 NaN NaN NaN
2009-01-21 NaN -4500 NaN 6000 NaN
答案 0 :(得分:2)
使用concat
和groupby.prod
:
df = pd.concat([df1,df2])
df.groupby(df.index).prod(min_count=1)
GOOG AAPL XOM IBM Cash
Date
2009-01-14 NaN 15000 NaN NaN NaN
2009-01-21 NaN -4500 NaN 6000.0 NaN
答案 1 :(得分:1)
使用DataFrame.mul
应该处理索引:
SELECT s1.`group` AS groupId1, (
SELECT id
FROM `students` s2
WHERE s2.`group` = s1.`group`
ORDER BY s2.grade DESC, s2.id ASC
LIMIT 0,1
) AS bestGradeId, (
SELECT id
FROM `students` s2
WHERE s2.`group` = s1.`group`
ORDER BY s2.grade DESC, s2.id ASC
LIMIT 1,1
) AS best2GradeId, (
SELECT id
FROM `students` s2
WHERE s2.`group` = s1.`group`
ORDER BY s2.grade DESC, s2.id ASC
LIMIT 2,1
) AS best3GradeId
FROM `students` s1
GROUP BY s1.`group`