如何在Pandas Python中连接不同的文本行?

时间:2019-08-06 07:45:53

标签: python pandas dataframe

我有一个如下所示的数据框:

Date 1-Jan 2-Jan 3-Jan 4-Jan
Duty
1    Bob   NaN   Bob   NaN
2    NaN   NaN   Bob   Bob
3    NaN   NaN   Bob   Bob

我要实现的是在每个日期后添加最后一行,如果任何Bob中有Duty,则该值将为“ Bob”,如下所示:

Date 1-Jan 2-Jan 3-Jan 4-Jan
Duty
1    Bob   NaN   Bob   NaN
2    NaN   NaN   Bob   Bob
3    NaN   NaN   Bob   Bob
sum  Bob   NaN   Bob   Bob

我尝试使用pd.apply,如下所示:

df.loc['sum'] = df.apply(lambda x: x.sum())

但是我得到的结果是:

Date 1-Jan 2-Jan 3-Jan     4-Jan
Duty
1    Bob   NaN   Bob       NaN
2    NaN   NaN   Bob       Bob
3    NaN   NaN   Bob       Bob
sum  NaN   NaN   BobBobBob NaN

我还尝试将每一行分成几个小的Dataframe,并尝试pd.merge(),这也不起作用。

2 个答案:

答案 0 :(得分:3)

首先,我们检查每一列中的值any是否等于eqBob)。然后,我们在映射appendTrue > Bob的同时False > NaN将这些值放在数据框的底部:

m = df.eq('Bob').any(axis=0).map({True: 'Bob', False: np.NaN})
df = df.append(pd.DataFrame(m, columns=['sum']).T)

输出

    1-Jan 2-Jan 3-Jan 4-Jan
0     Bob   NaN   Bob   NaN
1     NaN   NaN   Bob   Bob
2     NaN   NaN   Bob   Bob
sum   Bob   NaN   Bob   Bob

答案 1 :(得分:1)

如果需要每列第一个非缺失值,请使用fn translate_original(renderables: &mut Vec<f32>, tx: f32, ty: f32) { // This works let mut i = 0; while i < renderables.len() { translate_renderable(renderables, i, tx, ty); i = next_renderable(renderables, i); } } fn translate_iterator(renderables: &mut Vec<f32>, tx: f32, ty: f32) { // This fails RenderableIterator::new(renderables) .for_each(|i| translate_renderable(renderables, i, tx, ty)); } fn main() { let mut data_1: Vec<f32> = vec![ // renderable 1 2., 10., 10., 20., 20., // renderable 2 3., 30., 30., 40., 40., 50., 50. ]; let mut data_2 = data_1.clone(); translate_original(&mut data_1, 5., 5.); translate_iterator(&mut data_2, 5., 5.); } // Set up fn next_renderable(renderables: &Vec<f32>, i: usize) -> usize { i + 1 + (renderables[i] as usize) } fn translate_renderable(renderables: &mut Vec<f32>, i: usize, tx: f32, ty: f32) { let from = i + 1; let to = next_renderable(renderables, i); for i in (from..to).step_by(2) { renderables[i] += tx; renderables[i + 1] += ty; } } pub struct RenderableIterator<'a> { renderables: &'a Vec<f32>, i: usize, } impl RenderableIterator<'_> { pub fn new(renderables: &Vec<f32>) -> RenderableIterator { RenderableIterator { renderables, i: 0 } } } impl Iterator for RenderableIterator<'_> { type Item = usize; fn next(&mut self) -> Option<usize> { if self.i < self.renderables.len() { let out = Some(self.i); self.i = next_renderable(&self.renderables, self.i); out } else { None } } } nextSeries.dropna作为可能的默认值(如果不存在第一个值):

iter

或将DataFrame.stackGroupBy.first一起使用:

df.loc['sum'] = df.apply(lambda x: next(iter(x.dropna()),np.nan))
print (df)
     1-Jan  2-Jan 3-Jan 4-Jan
Date                         
1      Bob    NaN   Bob   NaN
2      NaN    NaN   Bob   Bob
3      NaN    NaN   Bob   Bob
sum    Bob    NaN   Bob   Bob

如果需要测试值df.loc['sum'] = df.stack().groupby(level=1).first() print (df) 1-Jan 2-Jan 3-Jan 4-Jan Date 1 Bob NaN Bob NaN 2 NaN NaN Bob Bob 3 NaN NaN Bob Bob sum Bob NaN Bob Bob

Bob