Create a DataFrame birds from this dictionary data which has the index labels

前端 未结 4 1816
旧巷少年郎
旧巷少年郎 2021-01-24 08:13

Consider the following Python dictionary data and Python list labels:**

data = {\'birds\': [\'Cranes\', \'Cranes\', \'plovers\', \'spoonbills\', \'spoonbills\',          


        
4条回答
  •  無奈伤痛
    2021-01-24 08:44

    Assuming your dictionary is already ordered into the correct ordering for the labels

    import pandas as pd
    
    data = {'birds': ['Cranes', 'Cranes', 'plovers', 'spoonbills', 'spoonbills', 'Cranes', 'plovers', 'Cranes', 'spoonbills', 'spoonbills'],
        'age': [3.5, 4, 1.5, np.nan, 6, 3, 5.5, np.nan, 8, 4],
        'visits': [2, 4, 3, 4, 3, 4, 2, 2, 3, 2],
        'priority': ['yes', 'yes', 'no', 'yes', 'no', 'no', 'no', 'yes', 'no', 'no']}
    
    data['labels'] = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
    
    df = pd.DataFrame(data, columns=['birds', 'age', 'visits', 'priority', 'labels'])
    df.set_index('labels')
    

提交回复
热议问题