python - 在pandas python数据框中移动列并删除行
问题描述
我有一个像这样的数据框 df
A B C D E F G H
a.1 b.1
c.1 d.1
c.2 d.2 e.1 f.1
g.1 h.1
创建示例数据框
from io import StringIO
s = """A,B,C,D,E,F,G,H
a.1,b.1,,,,,,
,,c.1,d.1,,,,
,,c.2,d.2,e.1,f.1,,
,,,,,,g.1,h.1"""
df = pd.read_csv(StringIO(s))
我想删除这些额外的空格,并且我希望数据框从第一行开始。任何人都可以帮忙。
我想要的结果是
A B C D E F G H
a.1 b.1 c.1 d.1 e.1 f.1 g.1 h.1
c.2 d.2
解决方案
您可以通过以下方式找到的前面缺失值的数量向后移动每一列first_valid_index
:
df.apply(lambda s: s.shift(-s.first_valid_index()))
要得到
A B C D E F G H
0 a.1 b.1 c.1 d.1 e.1 f.1 g.1 h.1
1 NaN NaN c.2 d.2 NaN NaN NaN NaN
2 NaN NaN NaN NaN NaN NaN NaN NaN
3 NaN NaN NaN NaN NaN NaN NaN NaN
要删除充满NaN
s 的行并用空字符串填充其余行:
out = (df.apply(lambda s: s.shift(-s.first_valid_index()))
.dropna(how="all")
.fillna(""))
要得到
>>> out
A B C D E F G H
0 a.1 b.1 c.1 d.1 e.1 f.1 g.1 h.1
1 c.2 d.2
注意:这假设您的索引是0..N-1
; 所以如果不是,您可以预先存储它然后恢复:
index = df.index
df = df.reset_index(drop=True)
df = (df.apply(lambda s: s.shift(-s.first_valid_index()))
.dropna(how="all")
.fillna(""))
df.index = index[:len(df)]
要使上拉特定于某些列:
def pull_up(s):
# this will be a column number; `s.name` is the column name
col_index = df.columns.get_indexer([s.name])
# for example: if `col_index` is either 7 or 8, pull by 4
if col_index in (7, 8):
return s.shift(-4)
else:
# otherwise, pull as much
return s.shift(-s.first_valid_index())
# applying
df.apply(pull_up)
推荐阅读
- reactjs - 如何使用下拉选项在 Select 组件上正确设置 aria-labels
- sql-server - SQL Server 复杂排名
- asp.net - ASP Net core ajax 无法识别传递的数据
- pentaho - 无法使用 PRPT(Kettle Job)生成报告
- keras - NLP 的 RNN Keras 模型在训练时需要大量时间,而验证损失没有减少
- java - 忽略令牌正则表达式中的字符串
- javascript - React-Native 中的异步存储
- javascript - 无法分配 Array#filter 的结果?
- python - 如何将字符串从 python3.6 转换为旧版本?
- sql-server - 在 SQL Server 中转换 bigint 纪元