给出两个数组:
x
[('010_628', '2543677'), ('010_228', '2543677'), ('015_634', '2543677')]
y
array([['me', 10228955],
['me', 10228955],
['me', 10228955]], dtype=object)
目前,这段代码为我提供了一个平面索引为元组的数据框:
df = pd.DataFrame(x, index=y, columns=['pm_code', 'sec_pm'])
df
pm_code sec_pm
(me, 10228955) 010_628 2543677
(me, 10228955) 010_228 2543677
(me, 10228955) 015_634 2543677
如何创建一个看起来像这样的MultiIndex
数据框?
pm_code sec_pm
state site_no
me 10228955 010_628 2543677
010_228 2543677
015_634 2543677
我尝试使用pd.MultiIndex.from_tuples
,但我无法做到这一点。谢谢你的帮助。
附录:效果比较
# unutbu #1
%timeit pd.DataFrame(x, index=pd.MultiIndex.from_arrays(y.T), columns=['pm_code', 'sec_pm'])
1000 loops, best of 3: 1.25 ms per loop
# unutbu #2
%timeit pd.DataFrame(x, index=pd.MultiIndex.from_tuples(y.tolist()), columns=['pm_code', 'sec_pm'])
1000 loops, best of 3: 1.47 ms per loop
# piRSquared
%timeit pd.DataFrame(x, index=y.T.tolist(), columns=['pm_code', 'sec_pm'])
1000 loops, best of 3: 1.41 ms per loop
# Andrew L
%timeit pd.DataFrame(x, index=[y[:,0], y[:,1]], columns=['pm_code', 'sec_pm'])
1000 loops, best of 3: 1.29 ms per loop
x2 = np.repeat(x, 10000, 0)
y2 = np.repeat(x, 10000, 0)
# unutbu #1
%timeit pd.DataFrame(x2, index=pd.MultiIndex.from_arrays(y2.T), columns=['pm_code', 'sec_pm'])
100 loops, best of 3: 17.3 ms per loop
# unutbu #2
%timeit pd.DataFrame(x2, index=pd.MultiIndex.from_tuples(y2.tolist()), columns=['pm_code', 'sec_pm'])
10 loops, best of 3: 30.5 ms per loop
# piRSquared
%timeit pd.DataFrame(x2, index=y2.T.tolist(), columns=['pm_code', 'sec_pm'])
10 loops, best of 3: 37.2 ms per loop
# Andrew L
%timeit pd.DataFrame(x2, index=[y2[:,0], y2[:,1]], columns=['pm_code', 'sec_pm'])
100 loops, best of 3: 22 ms per loop
此question的数据。
答案 0 :(得分:5)
您可以使用pd.MultiIndex.from_arrays(y.T)
:
In [53]: pd.DataFrame(x, index=pd.MultiIndex.from_arrays(y.T), columns=['pm_code', 'sec_pm'])
Out[53]:
pm_code sec_pm
me 10228955 010_628 2543677
10228955 010_228 2543677
10228955 015_634 2543677
或pd.MultiIndex.from_tuples(y.tolist())
:
In [54]: pd.DataFrame(x, index=pd.MultiIndex.from_tuples(y.tolist()), columns=['pm_code', 'sec_pm'])
Out[54]:
pm_code sec_pm
me 10228955 010_628 2543677
10228955 010_228 2543677
10228955 015_634 2543677
答案 1 :(得分:2)
您还可以对数组进行切片并传递给awk -F '|' '"ABC" $2 "DEF" == $1 && "ABC" $2 "DEFZ"+[0-9] == $1 { print }' WHTFile.txt > QC2Valid.txt**
:
index
答案 2 :(得分:2)
选项1
如果你传递一个类似于数组的列表,那么构造函数就知道如何处理它。
pd.DataFrame(x, index=y.T.tolist(), columns=['pm_code', 'sec_pm'])
pm_code sec_pm
me 10228955 010_628 2543677
10228955 010_228 2543677
10228955 015_634 2543677