从列列中提取值

时间:2018-03-29 03:46:02

标签: python list pandas

我有以下数据框,列sequence的值是一个列表:

id      sequence
001    [A, B, C, E, F]
002    [A, C]
003    []
004    [D]

我想创建两个名为firstsecond_to_last的新列:first,指示sequence列中列表的第一个元素,second_to_last表示sequence列中列表的倒数第二个元素。我期待新的df如下:

id      sequence             first    second_to_last
001    [A, B, C, E, F]        A        E
002    [A, C]                 A        A
003    []                     None     None
004    [D]                    D        None

我尝试使用以下代码:

df['first'] = df['sequence'][0]
df['second_to_last'] = df['sequence'][-2]

但是出现了以下错误:

There was a problem running this cell
ValueError Length of values does not match length of index 
ValueErrorTraceback (most recent call last)
<ipython-input-9-f08abfd1f93c> in <module>()

----> 2 df['first'] = df['sequence'][0]
      3 df['second_to_last'] = df['sequence'][-2]
      4 df

/opt/conda/envs/python2/lib/python2.7/site-packages/pandas/core/frame.pyc in __setitem__(self, key, value)
   2427         else:
   2428             # set column
-> 2429             self._set_item(key, value)
   2430 
   2431     def _setitem_slice(self, key, value):

/opt/conda/envs/python2/lib/python2.7/site-packages/pandas/core/frame.pyc in _set_item(self, key, value)
   2493 
   2494         self._ensure_valid_index(value)
-> 2495         value = self._sanitize_column(key, value)
   2496         NDFrame._set_item(self, key, value)
   2497 

/opt/conda/envs/python2/lib/python2.7/site-packages/pandas/core/frame.pyc in _sanitize_column(self, key, value, broadcast)
   2664 
   2665             # turn me into an ndarray
-> 2666             value = _sanitize_index(value, self.index, copy=False)
   2667             if not isinstance(value, (np.ndarray, Index)):
   2668                 if isinstance(value, list) and len(value) > 0:

/opt/conda/envs/python2/lib/python2.7/site-packages/pandas/core/series.pyc in _sanitize_index(data, index, copy)
   2877 
   2878     if len(data) != len(index):
-> 2879         raise ValueError('Length of values does not match length of ' 'index')
   2880 
   2881     if isinstance(data, PeriodIndex):

ValueError: Length of values does not match length of index

提取列firstsecond_to_last的值的正确方法是什么?谢谢!

2 个答案:

答案 0 :(得分:3)

选项1
在处理pandas中的字符串/其他可变对象列时,您需要使用str访问器。

df['first'] = df['sequence'].str[0]
df['second_to_last'] = df['sequence'].str[-2]

df
   id         sequence first second_to_last
0   1  [A, B, C, E, F]     A              E
1   2           [A, C]     A              A
2   3               []   NaN            NaN
3   4              [D]     D            NaN

选项2
另一种选择是定义自己的函数来检索给定索引处的项目:

def get_value(d, i):
    try:
        return d[i]
    except IndexError:
        return np.nan

循环df.sequence

df['first'] = [get_value(d, 0) for d in df.sequence]
df['second_to_last'] = [get_value(d, -2) for d in df.sequence]

df

   id         sequence first second_to_last
0   1  [A, B, C, E, F]     A              E
1   2           [A, C]     A              A
2   3               []   NaN            NaN
3   4              [D]     D            NaN

答案 1 :(得分:2)

与寒冷的魔法str相比,这种方法效率不高

df['seq'].apply(lambda x : np.nan if not x else x[0])
Out[1328]: 
0      A
1      A
2    NaN
3      D
Name: seq, dtype: object
df['seq'].apply(lambda x : np.nan if not x or len(x)<2 else x[-2])
Out[1329]: 
0      E
1      A
2    NaN
3    NaN
Name: seq, dtype: object