更换并弄平Numpy阵列

时间:2019-07-02 14:32:59

标签: python python-3.x pandas numpy numpy-ndarray

我有形状为(2,1)的ndarray。 每个元素都是形状为(4)的ndarray 我想制作一个形状为(2,4)

的数据框

对象形状:

df.shape = (2,1)
df[0].shape = (1,)
df[0][0].shape = (4,)

例如:

df[0][0] = [1 2 2 4]
df[1][0] = [1 1 1 1]

我希望它看起来像这样:

df[0] = [1 2 2 4]
df[1] = [1 1 1 1]

3 个答案:

答案 0 :(得分:0)

您想要这样的东西吗?

df = pd.DataFrame(a.reshape((2, 4)))

df
   0  1  2  3
0  1  2  2  4
1  1  1  1  1

或者:

   WITH final_leg AS(
    SELECT y.* 
    FROM 
           (
           SELECT
                 y.shipment_id,
                 y.route_id,
                 max(leg_sequence_id) max_leg_sequence_id
           FROM posimorders.sc_execution_eu.o_detailed_routes_v2 y
           group by
                 1,2
           ) AS x
           INNER JOIN posimorders.sc_execution_eu.o_detailed_routes_v2 y
           on x.route_id = y.route_id and x.shipment_id = y.shipment_id and y.leg_sequence_id = x.max_leg_sequence_id
    ),

    dest_leg AS(
    SELECT y.* 
    FROM 
           (
           SELECT
                 y.shipment_id,
                 y.route_id,
                 min(leg_sequence_id) max_leg_sequence_id
           FROM 
                 posimorders.sc_execution_eu.o_detailed_routes_v2 y
                 LEFT JOIN warehouse_attributes w                  -- Joining to add origin country of origin FC
                 ON w.warehouse_id = y.leg_warehouse_id

           group by
                 1,2
           ) x
           INNER JOIN posimorders.sc_execution_eu.o_detailed_routes_v2 y
           on x.route_id = y.route_id and x.shipment_id = y.shipment_id and y.leg_sequence_id = x.max_leg_sequence_id
    ),

list_legs_ds AS(
SELECT t1.*, t2.* FROM

(SELECT leg_warehouse_id, SUM(pck_count) AS total_packages
FROM posimorders.sc_execution_eu.o_detailed_routes_v2
WHERE trunc(cpt_date) between '2019-06-16' and '2019-06-22'
    and leg_sequence_id = 0
    and leg_warehouse_id not like 'X%'
    and right(leg_warehouse_id,1) in ('1','2','3','4','5','6','7','8','9')    --Only SC and not Airports
group by 1
having sum(pck_count) > 50000
) t1

CROSS JOIN

(select distinct leg_warehouse_id AS lm_ds , destination_country_code
from posimorders.sc_execution_eu.o_detailed_routes_v2
where trunc(cpt_date) BETWEEN '2019-06-16' and '2019-06-22'
    and leg_ship_method LIKE 'AMZN_%_PRIME'
    ) t2
    )

    SELECT
           a.route_warehouse_id,
           --k.leg_warehouse_id leg_ware,
           --k.leg_warehouse_id lm_ds,
    from
           final_leg a 
           inner join dest_leg b 
           on a.shipment_id = b.shipment_id and a.route_id = b.route_id

           --RIGHT JOIN list_legs_ds k
           --on a.leg_warehouse_id = k.leg_ware -- and a.leg_ship_method = k.last_ds

答案 1 :(得分:0)

您可以使用另一个数据框重新排列,然后重新设置,例如:

df2 = pd.DataFrame([df[0][0], df[0][1]])
df = df2

更新:关于@Koren Levenbrown的评论

df = np.array([df[column][0] for column in df])

是另一种解决方案

答案 2 :(得分:0)

好像您有一个对象dtype数组(但是为什么叫df?):

In [150]: df = np.empty((2,1),object)                                                                           
In [151]: df[0,0] = np.array([1,2,2,4])                                                                         
In [152]: df[1,0] = np.array([1,1,1,1])                                                                         
In [153]: df                                                                                                    
Out[153]: 
array([[array([1, 2, 2, 4])],
       [array([1, 1, 1, 1])]], dtype=object)
In [154]: df.shape                                                                                              
Out[154]: (2, 1)
In [155]: df[0].shape                                                                                           
Out[155]: (1,)
In [156]: df[0,0].shape                                                                                         
Out[156]: (4,)

np.concatenate(或stack派生之一)可以连接数组的列表/可迭代数组,只要它们的大小匹配即可。

stack直接应用于df无效,因为它的形状为(2,1):

In [157]: np.stack(df)                                                                                          
Out[157]: 
array([[array([1, 2, 2, 4])],
       [array([1, 1, 1, 1])]], dtype=object)

但是,如果我们首先拆散(或挤压)数组,使其变为(2,)形状:

In [158]: np.stack(df.ravel())                                                                                  
Out[158]: 
array([[1, 2, 2, 4],
       [1, 1, 1, 1]])