简体   繁体   中英

coping values from one dataframe to another dataframe

I have two pandas data frames df1 and df2 . df1 has 3 rows and 3 columns and df2 has 5 rows and 2 columns. I want to create a new column named 'close' for df2 like that in df1 .
I want to copy the df1 'close' column element to df2 where the df1 date, index and time columns are same with df2 date, index, and time columns.

Below I show the dataframe I want to achieve ( df3 ):

time= [datetime.time(10, 7, 59),
       datetime.time(15, 8, 59),
      datetime.time(11, 56, 59)]

date = ['7/2/2019',  '7/3/2019', '7/5/2019']
close = [55,65,75]

Date = ['7/2/2019', '7/2/2019', '7/3/2019', '7/5/2019', '7/5/2019']

Time = [datetime.time(10, 7, 59),
      datetime.time(10, 7, 59),
      datetime.time(15, 8, 59),
      datetime.time(11, 56, 59),
      datetime.time(11, 56, 59)]

Strike = [10000, 10000, 12300, 12200, 12200]


df1 = pd.DataFrame({"Time":time,"close":close},index = date)
df2 = pd.DataFrame({"Time":Time,"strike":Strike},index = Date)

# so i want to achieve df3 below

df3 = pd.DataFrame({"Time":Time,"strike":Strike,"close":[55,55,65,75,75]},index = Date)

With you example data

Since the indexes of the two dataframes are matching, you can simply do

df2['close'] = df1['close']

and pandas will make exactly what you want. df2 will have a new column, and will look exactly as your df3 . No need to use the 'time' column here, since the dates (index) have always the same time.

A more general case

But let's consider the case where you have same date (index) but different times. For example, say that the times in your df2 are slightly different than the ones you posted:

Time = [datetime.time(10, 7, 59),
      datetime.time(13, 7, 59),
      datetime.time(15, 8, 59),
      datetime.time(11, 56, 59),
      datetime.time(14, 56, 59)]

In this case what you can do is to build, in each dataframe, new indexes considering both date and time (I don't know why you kept them separated in the first place).

Given your current setup, you can do:

ddf1 = df1.set_index(pd.to_datetime(df1.index.to_series() + ' ' + df1['Time'].apply(lambda x : x.strftime('%H:%m:%S'))))
ddf2 = df2.set_index(pd.to_datetime(df2.index.to_series() + ' ' + df2['Time'].apply(lambda x : x.strftime('%H:%m:%S'))))

and now, again:

ddf2['close'] = ddf1['close']

Now you will get some nan values in ddf2['close'] since values are copied only for exact matches (date and time) and some times are different.

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