Replace values of a numpy array by values from another numpy array

后端 未结 4 1053
隐瞒了意图╮
隐瞒了意图╮ 2020-12-18 09:26

i have a 1000 * 1000 numpy array with 1 million values which was created as follows :

>>import numpy as np
>>data = np.loadtxt(\'space_data.txt\         


        
4条回答
  •  余生分开走
    2020-12-18 09:50

    # Create a dataframe out of your 'data' array and make a dictionary out of your 'key' array. 
    import numpy as np
    import pandas as pd
    
    data = np.array([[ 13.,  15.,  15.],
                   [ 14.,  13.,  14. ],
                   [ 16.,  13.,  13. ]])
    data_df = pd.DataFrame(data)
    key  = dict({10 : 'S',11 : 'S', 12 : 'S', 13 : 'M',14:'L',15:'S',16:'S'})
    # Replace the values in newly created dataframe and convert that into array.
    data_df.replace(key,inplace = True)
    
    data = np.array(data_df)
    print(data)
    

    This will be the output:

    [['M' 'S' 'S']
    ['L' 'M' 'L']
    ['S' 'M' 'M']]
    

提交回复
热议问题