这段代码:
import pandas as pd
import numpy as np
df = pd.DataFrame(np.arange(25).reshape(5,5))
df[['A', 'B']] = np.arange(30,40).reshape(5,2)
在1.1.1中工作正常,但在1.0.3中引发错误
KeyError:“ [Index(['A','B'],dtype ='object')]都不在[列]中”
有没有办法以向后兼容的方式做到这一点?
使用T
转置阵列和使用拆包:
df['A'], df['B'] = np.arange(30,40).reshape(5,2).T
结果:
0 1 2 3 4 A B
0 0 1 2 3 4 30 31
1 5 6 7 8 9 32 33
2 10 11 12 13 14 34 35
3 15 16 17 18 19 36 37
4 20 21 22 23 24 38 39
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句