我将pandas数据帧设为:
#include <iostream>
using namespace std;
class Rational {
private:
int n; // (fraction numerator)
int d; // (fraction denominator).
public:
Rational() {
n = 0;
d = 1;
}
Rational(int num, int denom) {
n = num;
d = denom;
}
friend istream& operator >> (istream& in_str, Rational& right);
friend ostream& operator << (ostream& out_str, const Rational& right);
friend Rational operator+ (const Rational&, const Rational&);
//friend istream& operator >> (istream& in_str, Rational& right);
//friend ostream& operator <<(ostream& out_str, const Rational& right);
//friend Rational operator+(const Rational&, const Rational&);
};
int main()
{
cout << "Testing declarations" << endl;
cout << "Rational x, y(2), z(-5,-6), w(1,-3);" << endl;
return 0;
}
istream& operator >> (istream& in_str, Rational& right) {
cin >> n.right;
}
ostream& operator << (ostream& out_str, const Rational& right); {
cout << out_str;
}
Rational operator+(const Rational&, const Rational&); {
cout << Rational;
}
我想执行以下操作:
df1['A'].ix[1:3]
2017-01-01 02:00:00 [33, 34, 39]
2017-01-01 03:00:00 [3, 43, 9]
df2['B'].ix[1:3]
2017-01-01 02:00:00 2
2017-01-01 03:00:00 3
预期结果是:
difference = df1 - df2
即。来自(df1 - df2).ix[1:3]
2017-01-01 02:00:00 [31, 32, 37]
2017-01-01 03:00:00 [0, 40, 6]
df2
中的减去数字
df1
的每个列表都有相同的大小。
我无法想到比df1
更好的方式。
答案 0 :(得分:5)
我无法分辨df1
中的对象是什么。可能list
可能np.array
? IDK?
最好我创建自己的例子
A = pd.Series([[33, 34, 39], [3, 43, 9]])
B = pd.Series([2, 3])
选项1
apply(np.asarray)
A.apply(np.asarray) - B
0 [31, 32, 37]
1 [0, 40, 6]
dtype: object
选项2
更丑但更快,见下文
pd.Series((np.array(A.values.tolist()) - B.values[:, None]).tolist(), A.index)
0 [31, 32, 37]
1 [0, 40, 6]
dtype: object
<强> 定时 强>
答案 1 :(得分:3)
您可以将列值从列表转换为numpy数组,然后执行df1.A - df2.B
:
df1.A = df1.A.map(np.array)
difference = df1.A - df2.B