截断pandas DataFrame的行

时间:2017-06-14 15:12:58

标签: python-2.7 pandas dataframe truncate

创建示例数据帧的代码:

/_api/Web/

示例数据框可视化:

Sample = [{'account': 'Jones LLC', 'Jan': 150, 'Feb': 200, 'Mar': [[.332, .326], [.058, .138]]},
     {'account': 'Alpha Co',  'Jan': 200, 'Feb': 210, 'Mar': [[.234, .246], [.234, .395], [.013, .592]]},
     {'account': 'Blue Inc',  'Jan': 50,  'Feb': 90,  'Mar': [[.084, .23], [.745, .923], [.925, .843]]}]
df = pd.DataFrame(Sample)

我正在寻找一个截断'Mar'列的公式,以便截断形状大于(2,x)的任何行,从而产生以下df

 df:
  account        Jan      Feb          Mar
Jones LLC  |     150   |   200    | [.332, .326], [.058, .138]
Alpha Co   |     200   |   210    | [[.234, .246], [.234, .395], [.013, .592]]
Blue Inc   |     50    |   90     | [[.084, .23], [.745, .923], [.925, .843]]

3 个答案:

答案 0 :(得分:5)

str访问器是为字符串操作而设计的,但是对于像列表这样的迭代,你也可以用它来进行切片:

df['Mar'] = df['Mar'].str[:2]

df
Out: 
   Feb  Jan                               Mar    account
0  200  150  [[0.332, 0.326], [0.058, 0.138]]  Jones LLC
1  210  200  [[0.234, 0.246], [0.234, 0.395]]   Alpha Co
2   90   50   [[0.084, 0.23], [0.745, 0.923]]   Blue Inc

答案 1 :(得分:3)

使用.apply函数与lambda运算符结合,可以轻松完成单元格级别的操作:

df["Mar"] = df["Mar"].apply(lambda x: x[:2])

答案 2 :(得分:2)

Pandas在系列列表中的表现并不是很好,所以在开始工作之前将它拉出来可能会更好:

df['Mar'] = [row[:2] for row in df['Mar'].tolist()]

%timeit结果以及ayhan和Marjan的出色答案:

3行:

%timeit df['Mar'].str[:2]
10000 loops, best of 3: 154 µs per loop

%timeit df['Mar'].apply(lambda x: x[:2])
10000 loops, best of 3: 133 µs per loop

%timeit [row[:2] for row in df['Mar'].tolist()]
The slowest run took 5.51 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 12 µs per loop

>>>5.51*12
66.12

3,000,000行:

%timeit df['Mar'].str[:2]
1 loop, best of 3: 1.23 s per loop

%timeit df['Mar'].apply(lambda x: x[:2])
1 loop, best of 3: 1.25 s per loop

%timeit [row[:2] for row in df['Mar'].tolist()]
1 loop, best of 3: 940 ms per loop

如果您愿意将列表对拆分为两列,您可以使用

获得一种比上述更好地扩展到更大数据帧的方法
>>>pd.DataFrame(df['Mar'].tolist()).iloc[:, :2]]
                0               1
0  [0.332, 0.326]  [0.058, 0.138]
1  [0.234, 0.246]  [0.234, 0.395]
2   [0.084, 0.23]  [0.745, 0.923]

在3,000,000行:

%timeit pd.DataFrame(df['Mar'].tolist()).iloc[:, :2]
1 loop, best of 3: 276 ms per loop