数据:
A B
0 a d
1 b NaN
2 c c
3 d f
我如何在A和B之间交错0列?我想得到:
A X B
0 a 0 d
1 b 0 NaN
2 c 0 c
3 d 0 f
同样,为此:
A B C
0 a d e
1 b NaN a
2 c c f
3 d f g
我想:
A X B X C
0 a 0 d 0 e
1 b 0 NaN 0 a
2 c 0 c 0 f
3 d 0 f 0 g
我该怎么做?
您可以在下面找到我的尝试。我想知道其他的,可能更短/更有效/古怪的方式。
答案 0 :(得分:4)
1)构建列的一种方法
In [916]: df.reindex(columns=sum([[c, 'X'] for c in df.columns], [])[:-1], fill_value=0)
Out[916]:
A X B X C
0 a 0 d 0 e
1 b 0 NaN 0 a
2 c 0 c 0 f
3 d 0 f 0 g
In [917]: sum([[c, 'X'] for c in df.columns], [])[:-1]
Out[917]: ['A', 'X', 'B', 'X', 'C']
2)构建
的另一种方式In [924]: def mixcols(cols):
...: ncols = ['X'] * 2 * len(cols)
...: ncols[0::2] = cols
...: return ncols[:-1]
In [925]: mixcols(df.columns)
Out[925]: ['A', 'X', 'B', 'X', 'C']
In [926]: df.reindex(columns=mixcols(df.columns), fill_value=0)
Out[926]:
A X B X C
0 a 0 d 0 e
1 b 0 NaN 0 a
2 c 0 c 0 f
3 d 0 f 0 g
3)从@piR启发
In [944]: [c for cx in zip(df.columns, ['X']*len(df.columns)*2) for c in cx][:-1]
Out[944]: ['A', 'X', 'B', 'X', 'C']
此外,
In [939]: from itertools import chain, izip
In [940]: list(chain.from_iterable(izip(df.columns, ['X']*len(df.columns)*2)))[:-1]
Out[940]: ['A', 'X', 'B', 'X', 'C']
4)不推荐的方式
In [935]: '-|-X-|-'.join(df.columns).split('-|-')
Out[935]: ['A', 'X', 'B', 'X', 'C']
答案 1 :(得分:3)
选项0
df.reindex_axis([x for y in lst for x in [y, 'X']][:-1], 1, fill_value=0)
A X B X C
0 a 0 d 0 e
1 b 0 NaN 0 a
2 c 0 c 0 f
3 d 0 f 0 g
选项1
字符串hackery
实际上,@约翰高尔特打败了我。
df.reindex_axis('|X|'.join(df).split('|'), 1, fill_value=0)
A X B X C
0 a 0 d 0 e
1 b 0 NaN 0 a
2 c 0 c 0 f
3 d 0 f 0 g
选项2
使用cytoolz.interleave
从 @root here
from cytoolz import interleave
df.reindex(
columns=list(interleave([df, list('X' * (df.shape[1] - 1))])),
fill_value=0)
A X B X C
0 a 0 d 0 e
1 b 0 NaN 0 a
2 c 0 c 0 f
3 d 0 f 0 g
答案 2 :(得分:1)
我尝试使用df.reindex
:
def foo(cols):
yield cols[0]
for c in cols[1:]:
yield from ('X', c)
df1.reindex(columns=foo(df1.columns), fill_value=0)
A X B
0 a 0 d
1 b 0 NaN
2 c 0 c
3 d 0 f