简体   繁体   中英

'>=' not supported in piecewise operation 'numpy.ndarray' and 'str'

I have two dataframes:

df_1 df_2

I am trying to get the 'camp' from df_1 in df_2 if the df_2.date>=df_1.startDate and df_2.date

I have tried piecewise operation:

condition = [(df_2.date.values >= start_date)&(df_2.date.values <= end_date) for start_date, end_date in zip(df_1.startDate.values, df_1.endDate.values)]
df_2['ID_matched'] = np.piecewise(np.zeros(len(sales)), condition, df_1.camp.values)

I am getting this error:

'>=' not supported between instances of 'numpy.ndarray' and 'str'

Is there any other way to solve this. Final output should contain these columns: sale,date,camp

I could get an answer to this solution.

1. Created a list with unique camp names from df_1 and converted it to a dictionary with camp as key and numbers as elements.

unique_campaign = list(set(df_1['camp'])) dictionary = dict(zip(df_1, list(range(1,len(unique_campaign)+1))))

2. Using applymap converted the camp name to numbers from dictionary.

df_1= df_1.applymap(lambda x: dictionary.get(x) if x in dictionary else x)

3. np.piecewise operation

start_end_date = zip(df_1.startDate.values, df_1.endDate.values)

conditions = [(df_2.date.values >= start_date) & (df_2.date.values <= end_date) for start_date, end_date in start_end_date ]

df_2['camp'] = np.piecewise(np.zeros(len(df_2)), conditions,df_1.camp.values).astype(int)

4. By step 3 we have got the answer as an int. Now we map the integer from the dictionary to get the camp name.

inv_dictionary = {values: keys for keys, values in dictionary.items()}

df_2['camp'] = df_2['camp'].apply(lambda x: inv_dictionary.get(x) if x in inv_dictionary else x)

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