【问题标题】:Python, Pandas: Using isin() like functionality but do not ignore duplicates in input listPython,Pandas:使用类似 isin() 的功能,但不要忽略输入列表中的重复项
【发布时间】: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]])

谢谢!

【问题讨论】:

  • 显示您希望看到的实际输出,以便我们更好地理解

标签: python pandas numpy


【解决方案1】:

查看reindex

df_out_desired = df_in.reindex(indices_needed_list)
df_out_desired 
Out[177]: 
   A   B
1  2  20
2  3  30
3  4  40
3  4  40
3  4  40

【讨论】:

    猜你喜欢
    • 2021-04-11
    • 1970-01-01
    • 2020-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-18
    • 1970-01-01
    • 2017-01-11
    相关资源
    最近更新 更多