简体   繁体   中英

copy data from one dataframe to another dataframe

for index,row in dr.iterrows():
    if(dr['Level :'][index].day == today.day or dr['Level :'][index].day == tomo.day ):
        dr1 = row
    else:
        dr2 = row

Actually, I'm new to this pandas concept. As I copy data from dr dataframe to dr2 dataframe based on condition. Those rows in dr dataframe are getting copied as columns in dataframe dr2 . So, what change should I make in my code to retain dr2 dataframe shape same as dr dataframe.

You can select row indeces, that satisfy the condition. Then use those to split the initial dataframe (I guess, you want to reset_index() as well):

dr["day"] = dr["Level :"].apply(lambda x: x.day)

ix = dr["day"].isin([today.day, tomo.day])
dr1 = dr.loc[ix].reset_index(drop=True)
dr2 = dr.loc[~ix].reset_index(drop=True)

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