Home>

I want to resolve TypeError: unhashable type:'numpy.ndarray'
Please write the details of the question here.
In Python3
I would like to sort two columns in ascending order like Excel, and rearrange the order in the other column
Should I make a companion into a dictionary?

Error message
TypeError: unhashable type: 'numpy.ndarray'
Applicable source code
import pandas as pd
import numpy as np
# Load file
# Need to match the environment
df = pd.read_csv (r'C: \ pytrategy \ mfmdb02.csv ', encoding = "cp932", index_col = 0, engine =' python ')
triggers = ['gap']
targets = ['gap1']
xa = df [triggers] .as_matrix () [1: -1]
ya = df [targets] .as_matrix () [1: -1]
za = dict (zip (xa, ya))
  • Answer # 1

    It may not be the answer you want, but ...

      

    I want to sort two columns in ascending order like Excel, and rearrange the order in another column

    If you don't have a bad dictionary,

    df = df.sort_values ​​('gap')
    print (df)
    Isn't it good to use

    ?

  • Answer # 2

    How about this?

    import numpy as np
    original = 1 + np.arange (5)
    order = np.array ([1, 2, 3, 0, 4])
    index = np.argsort (order)
    sorted = original [index]
    print (original)
    print (order)
    print (index)
    print (sorted)

    original = targets
    order = triggers
    I hope you can.