如何比较数据框列中的字符串值和单元格中的值以基于多值字典创建新的数据框?

时间:2019-01-07 21:23:24

标签: python python-3.x pandas dataframe multi-value-dictionary

我有一个如下所示的df:

id.1.value.1        id.2.value.2      id.1.question    id.2.value.2
TRUE                     FALSE             TRUE             TRUE

我想创建一个逻辑来扫描df的列名,并仅从列名中有value的列名中提取最后一个数字,然后比较包含{{1 }}具有以下逻辑:

  1. 如果列value中的值等于value,则比较多值字典中的最后一个数字

  2. 使用多键字典中的第二个值创建数据框列名称

示例:

TRUE

如果我当前的df看起来像这样:

my_dict = {1: ('a', 'category'),2: ('b', 'category'),\
           3: ('c', 'category'),4:('d','category'),\
           5:('e','subcategory'),6:('f','subcategory'),\
           7:('g','subcategory'),8:('h','subcategory'),\
           9:('i','subcategory'),10:('j','subcategory'),\
           11:('k','subcategor'),12:('l','subcategory'),\
           13:('m','subcategory'),14:('n','subcategory'),\
           15:('o','subcategory'),16:('p','subcategory'),\
           17:('q','subcategory'),18:('r','subcategory'),\
           19:('s','subcategory'),20:('t','subcategory'),\
           21:('u','subcategory'),22:('v','subcategory'),\
           23:('w','subcategory'),24:('x','subcategory')

           }

新df应该如下所示:

id.1.value.1        id.2.value.2      id.1.question    id.6.value.6
    TRUE                 FALSE             TRUE             TRUE

4 个答案:

答案 0 :(得分:1)

names = df.columns
new_df = pd.DataFrame()
for name in names:    
    if ('value' in name) & df[name][0]:
        last_number = int(name[-1])
        key, value = my_dict[last_number]
        try:
            new_df[value][0] = list(new_df[value][0]) + [key]
        except:
            new_df[value] = [key]      

答案 1 :(得分:0)

其中df

<script>

使用:

react-scripts

输出:

   id.1.value.1  id.2.value.2  id.1.question  id.6.value.6
0          True         False           True          True

答案 2 :(得分:0)

new_df = pd.DataFrame()

# get column names
for col in (list(df)):

    if "value" in col:

        try:
            # operate only in columns where a valid number is found
            value = df[col].rpartition('.')[:-1]

            # When df== True
            if df.loc[col,1]==True:
                new_df[my_dict[value][1]]= my_dict[value][0]
        except Exception as e:
            print(e)

答案 3 :(得分:0)

IIUC:

ans = [my_dict[int(x[-1])] for x in df1.where(df1.loc[:,['value' in x for x in df1.columns]]).dropna(axis=1)]
pd.DataFrame.from_dict({v: k for k, v in dict(ans).items()}, orient='index').T

输出:

  category subcategory
0        a           f