pandas - 选择并将列名称转换为值

时间:2017-08-07 12:55:38

标签: python pandas dataframe

从此数据框开始df:

df = pd.DataFrame({'id':[1,2,3,4],'a':['on','on','off','off'], 'b':['on','off','on','off']})

     a    b  id
0   on   on   1
1   on  off   2
2  off   on   3
3  off  off   4

我想要实现的是一个列result,其结果来自列的“开”和“关”选择。预期产出是:

     a    b  id result
0   on   on   1 [a,b]
1   on  off   2 [a]
2  off   on   3 [b]
3  off  off   4 []

所以基本上我必须在列中选择'on'值(id除外),然后将结果列名保存到列表中。我的第一次尝试是使用pivot_table

d = pd.pivot_table(df, index='id', columns=?, values=?)

但我仍然坚持如何将选择放入values和新列中的columns args。

4 个答案:

答案 0 :(得分:2)

对我来说,工作创建嵌套的list,然后按str[0]选择列表的第一个值:

df['res'] = df[['a','b']].eq('on').apply(lambda x: [x.index.values[x]], axis=1).str[0]
print (df)
     a    b  id     res
0   on   on   1  [a, b]
1   on  off   2     [a]
2  off   on   3     [b]
3  off  off   4      []

首先创建元组,然后转换为list s:

df['res'] = df[['a','b']].eq('on')
                         .apply(lambda x: tuple(x.index.values[x]), axis=1).apply(list)
print (df)
     a    b  id     res
0   on   on   1  [a, b]
1   on  off   2     [a]
2  off   on   3     [b]
3  off  off   4      []

答案 1 :(得分:1)

您也可以使用

代替数据透视表
df['result'] = df.iloc[:,0:2].eq('on').apply(lambda x: tuple(df.columns[0:2][x]), axis=1)

输出:

  a    b  id  result
0   on   on   1  (a, b)
1   on  off   2    (a,)
2  off   on   3    (b,)
3  off  off   4      ()

答案 2 :(得分:1)

或者您可以使用from django.views import generic from django.db.models import Q from yourapp.models import ModelName class SearchView(generic.ListView): template_name = 'yourapp/search.html' paginate_by = 10 def get_queryset(self): self.query = self.request.GET.get('q') # `search_text` try: return ModelName.objects.filter( Q(field1__icontains=self.query) | Q(field2__icontains=self.query) | Q(field3__icontains=self.query) ) #.order_by('-created') except: # handle if query is `None` return ModelName.objects.all() def get_context_data(self, **kwargs): context_data = super(SearchView, self).get_context_data(**kwargs) context_data['query'] = self.query return context_data eq

mul

答案 3 :(得分:0)

试试这个:

import pandas as pd
df = pd.DataFrame({'id':[1,2,3,4],'a':['on','on','off','off'], 'b':['on','off','on','off']})
stringList = []
for i in range(0,df.shape[0]):
    if df['a'][i] == 'on' and df['b'][i] == 'on':
        stringList.append('[a,b]')
    elif df['a'][i] == 'on' and df['b'][i] == 'off':
        stringList.append('[a]')
    elif df['a'][i] == 'off' and df['b'][i] == 'on':
        stringList.append('[b]')
    else:
        stringList.append('[]')
df['result'] = stringList
print df