基于此SO question,我想基于列var1
拆分数据框。但是,我在字母之间没有定界符。
import pandas as pd
a = pd.DataFrame([{'var1': 'abc', 'var2': 1},
{'var1': 'def', 'var2': 2}])
b = pd.DataFrame([{'var1': 'a', 'var2': 1},
{'var1': 'b', 'var2': 1},
{'var1': 'c', 'var2': 1},
{'var1': 'd', 'var2': 2},
{'var1': 'e', 'var2': 2},
{'var1': 'f', 'var2': 2}])
这是我想要实现的。
>>> a
var1 var2
0 abc 1
1 def 2
>>> b
var1 var2
0 a 1
1 b 1
2 c 1
3 d 2
4 e 2
5 f 2
.split()
不适用于空字符(“”)。
pd.concat([Series(row['var2'], row['var1'].split(','))
for _, row in a.iterrows()]).reset_index()
因此,以上操作无效。知道我该如何实现吗?
答案 0 :(得分:2)
使字符串成为列表:
pd.concat([pd.Series(row['var2'], list(row['var1'])) for _, row in a.iterrows()]).reset_index()
答案 1 :(得分:2)
如果性能很重要,请使用列表理解:
df = pd.DataFrame([[x, j] for i, j in zip(a['var1'], a['var2']) for x in list(i)],
columns=a.columns)
print (df)
var1 var2
0 a 1
1 b 1
2 c 1
3 d 2
4 e 2
5 f 2
小型DataFrame中的性能
In [215]: %timeit pd.DataFrame([[x, j] for i, j in zip(a['var1'], a['var2']) for x in list(i)], columns=a.columns)
355 µs ± 4.08 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
In [216]: %timeit pd.concat([pd.Series(row['var2'], list(row['var1'])) for _, row in a.iterrows()]).reset_index()
2.93 ms ± 203 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
#2k rows
a = pd.concat([a] * 1000, ignore_index=True)
In [217]: %timeit pd.DataFrame([[x, j] for i, j in zip(a['var1'], a['var2']) for x in list(i)], columns=a.columns)
2.82 ms ± 23.4 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
In [218]: %timeit pd.concat([pd.Series(row['var2'], list(row['var1'])) for _, row in a.iterrows()]).reset_index()
1.8 s ± 140 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
编辑:
多列的通用解决方案:
a = pd.DataFrame([{'var1': 'abc', 'var2': 1, 'var3':7},
{'var1': 'def', 'var2': 2, 'var3':5}])
b = pd.DataFrame([(y, *x[1:]) for x in a.values.tolist() for y in list(x[0])],
columns=a.columns)
print (b)
var1 var2 var3
0 a 1 7
1 b 1 7
2 c 1 7
3 d 2 5
4 e 2 5
5 f 2 5
#lower python versions
b = pd.DataFrame([(y,) + tuple(x[1:]) for x in a.values.tolist() for y in list(x[0])],
columns=a.columns)
print (b)
var1 var2 var3
0 a 1 7
1 b 1 7
2 c 1 7
3 d 2 5
4 e 2 5
5 f 2 5