如何通过审核字段在熊猫中创建摘要行

时间:2019-08-07 23:11:37

标签: python pandas

我试图根据原始输入派生单个行,然后在不同的时间点对各个列值进行各种更改。我简化了下面的列表。

I have read in some data into my dataframe as so:

    A    B    C  D   E

0   h    h    h  h   h 
1   x
2        y       1    
3             2  3


row 0 - "h" represents my original record.
rows 1 - 3 are changes over time to a specific column

I would like to create a single "result row" that would look something like:

'x', 'y, '2', '3' 'h'

有没有一种简单的方法可以在没有过多循环的情况下使用Pandas和Python?

2 个答案:

答案 0 :(得分:1)

您可以像这样获得列表:

>>> [df[s][df[s].last_valid_index()] for s in df]
['x', 'y', 2, 3, 'h']

如果需要附加它或带有名称的东西,则需要为其提供索引,然后附加它,就像这样:

df.append(pd.Series(temp, index=df.columns, name='total'))
# note, this returns a new object
# where 'temp' is the output of the code above

答案 1 :(得分:0)

您可以尝试

#df=df.replace({'':np.nan})
df.ffill().iloc[[-1],:]