简体   繁体   中英

How can I create a Pandas Dataframe column from another Pandas Timeseries?

I have a Dataframe with Datetime columns:

Parcela     DS1        DC1         DS0        DC0
P1      2016-04-26  2016-09-26  2016-04-11  2016-09-11
P2      2016-04-26  2016-09-26  2016-04-11  2016-09-11

I've tried to create a new column with the following code:

df['sem'] = prec[df['DS0'].dt.strftime('%Y-%m%d'):df['DS1'].dt.strftime('%Y-%m-%d')].sum()

where prec is another dataframe with a datetime index,

 Datetime               prec
 2016-04-13 00:00:00    0.0
 2016-04-13 00:10:00    0.0

but I get the following error

Cannot convert input of type <class 'pandas.core.series.Series'> to Timestamp

Could you help me?

您可以向

df['sem'] = [prec.loc[x:y,'prec'].sum() for x , y in zip(df['DS0'].dt.strftime('%Y-%m%d'),df['DS1'].dt.strftime('%Y-%m-%d'))]

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM