I have two data frames:
dt1:
date value
0 20000101 2
1 20100101 5
dt2:
date value
0 20000101 1
1 20100101 1
The new data frame is the subtraction of dt1.value and dt2.value:
date value
0 20000101 1
1 20100101 4
How to do that ?
You can try setting index
on index
and date
columns and subtract two dataframe
:
dt = (dt1.set_index(['index', 'date'])- dt2.set_index(['index', 'date'])).reset_index()
dt
Result:
index date value
0 0 20000101 1
1 1 20100101 4
Or, you can copy dt1
to new dataframe and update the value
column by result of subtraction:
dt = dt1.copy()
dt['value'] = dt1['value'] - dt2['value']
Looking at timeit
, the copy
and updating value seems much faster than indexing, subtracting, and resetting index:
%%timeit
dt = (dt1.set_index(['index', 'date'])- dt2.set_index(['index', 'date'])).reset_index()
dt
Result:
100 loops, best of 3: 4.35 ms per loop
For copy
:
%%timeit
dt = dt1.copy()
dt['value'] = dt1['value'] - dt2['value']
Result:
1000 loops, best of 3: 371 µs per loop
Suppose you have:
df1=pd.DataFrame({'date': {0: 20000101, 1: 20100101}, 'value': {0: 2, 1: 5}})
df2=pd.DataFrame({'date': {0: 20000101, 1: 20100101}, 'value': {0: 1, 1: 1}})
You can use sbtract:
df1.assign(value=df1.value-df2.value)
Out[253]:
date value
0 20000101 1
1 20100101 4
使用sub
dt1.set_index(["index", "date"]).sub(dt2.set_index(["index", "date"]), fill_value=0).reset_index()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.