Python. Numpy. Name of columns



  • Let's say there's a mass:

    a = np.array([[1.2, -3.5, 0., -10.],
                  [0.4, 2.1, -0.1, 0.5],
                  [0., 1.1, 1., 1.5]])
    

    How do you give the names of columns 1.2,3,4? I used it.

    a = np.insert(a, 0, [1, 2, 3, 4], 0)
    

    [[ 1. 2. 3. 4. ]
    [ 1.2 -3.5 0. -10. ]
    [ 0.4 2.1 -0.1 0.5]
    [ 0. 1.1 1. 1.5]]

    After that, I'd like to do some pole surgery, like, one and three pillars.

        [[  [1,3]   2.   4. ]
    [ 1.2 -3.5 -10.]
    [ 0.3 2.1 0.5]
    [ 1. 1.1 1.5]]

    Then do as follows:

        [[  [1,3,4]   2. ]
    [ -8.8 -3.5 ]
    [ 0.8 2.1 ]
    [ 2.5 1.1 ]]

    I want to see the poles united. I don't want a numpy. How can that be realized? Maybe some other libraries. I don't think it's good to ask dtypes, or I'm not good at it. But I'd like to use the numpy mass because of convenient functions.



  • Modul https://pandas.pydata.org/pandas-docs/stable/user_guide/10min.html Established to work with tabular data (2D and 1D sets). 2D Pandas tables DataFrame and represent a set of indicted and named pillars. Each pole under the hood is a 1D Numpy vector with a name and indices. In the Pandas, they are called Series

    Example:

    In [129]: a = np.array([[1.2, -3.5, 0., -10.],
         ...:               [0.4, 2.1, -0.1, 0.5],
         ...:               [0., 1.1, 1., 1.5]])
    

    In [130]: df = pd.DataFrame(a, columns=[1,2,3,4])

    In [131]: df
    Out[131]:
    1 2 3 4
    0 1.2 -3.5 0.0 -10.0
    1 0.4 2.1 -0.1 0.5
    2 0.0 1.1 1.0 1.5

    In [133]: res = pd.DataFrame({"sum_1_3_4": df[[1,3,4]].sum(axis=1), 2: df[2]})

    In [134]: res
    Out[134]:
    sum_1_3_4 2
    0 -8.8 -3.5
    1 0.8 2.1
    2 2.5 1.1



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2