简体   繁体   中英

pandas merge df and series

I want to filter a large dataframe with series. I hear join/merge is the fastest way. I want to filter the index of the dataframe using the values(not the index) from the series. here are my codes and errors

pd.merge(df_customer, interested_customers, left_index=True, how='inner')

error:

ValueError: can not merge DataFrame with instance of type <class 'pandas.core.series.Series'>

Here is the join function:

df_customer.join(interested_customers, how='inner', left_index=True)

Here is the error: join() got an unexpected keyword argument 'left_index'

If i take away the left_index i get an empty dataframe. Eventhough they are matching values

假设您将customer作为df_customer DF中的索引:

df_customer.loc[interested_customers]
pd.merge(df_customer, pd.DataFrame(interested_customers, columns = ['name_of_merging_column']), left_index=True, how='inner')

OR

df_customer.join(interested_customers.astype(int), how='inner', left_index=True)

OR

df_customer[df_customer.index.isin([interested_customers])]

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