python - Collapsing rows in a Dataframe into one specific row to fill missing values?
问题描述
I want to collapse values in all rows into the first (or specified) row
The dataframe looks like this:
id column1 column2 column3
A 15
B 13
C 10
I want it to look like this:
id column1 column2 column3
A 13 15 10
Apologies if my formatting sucks, this is my first submission.
解决方案
一个技巧是使用GroupBy.first
默认忽略 NA 值。
df[df.ne('')].groupby(np.zeros(len(df))).first()
另一个选项是''.join
在每一列中使用,因为这些空槽中有空字符串。
df.astype(str).apply(''.join).to_frame().T
id column1 column2 column3
0.0 A 13.0 15.0 10.0
推荐阅读
- lua - Roblox 如何检测玩家的团队
- python - 在嵌套的字符串列表中查找唯一元素
- html - rtf(富文本格式)base64数据在angular 6 html上显示
- php - 调用未定义的方法 Illuminate\Database\Eloquent\Builder::pullCache()
- reactjs - React 条件渲染中的表迭代
- typescript - 有条件继承可能吗?
- javascript - 如何将数据从 api 传递到 gojs TreeModel
- javascript - 使用 csv-parser 解析 csv 后如何获取输出?
- r - 如何将现有的 R 代码从随机样本的输出更改为所有可能结果的列表?
- verilog - 如何在 Verilog 中编程延迟?