【发布时间】:2018-07-13 22:13:10
【问题描述】:
我正在尝试根据索引列表过滤输入数据帧 (df_in)。索引列表包含重复项,我希望我的输出 df_out 包含特定索引的所有出现。正如预期的那样,isin() 只为每个索引提供了一个条目。
如何尝试不忽略重复项并获得类似于df_out_desired 的输出?
import pandas as pd
import numpy as np
df_in = pd.DataFrame(index=np.arange(4), data={'A':[1,2,3,4],'B':[10,20,30,40]})
indices_needed_list = pd.Series([1,2,3,3,3])
# In the output df, I do not particularly care about the 'index' from the df_in
df_out = df_in[df_in.index.isin(indices_needed_list)].reset_index()
# With isin, as expected, I only get a single entry for each occurence of index in indices_needed_list
# What I am trying to get is an output df that has many rows and occurences of df_in index as in the indices_needed_list
temp = df_out[df_out['index'] == 3]
# This is what I would like to try and get
df_out_desired = pd.concat([df_out, df_out[df_out['index']==3], df_out[df_out['index']==3]])
谢谢!
【问题讨论】:
-
显示您希望看到的实际输出,以便我们更好地理解