首页 > 解决方案 > I want to get 2 rows and 3 columns when dictionary is given

问题描述

Retrieving 2 rows and 3 column using pandas

but the unable to it works in csv file but how to do when dictionary is given!?

          import pandas as pd
          data =  {'Name':['Jai', 'Princi', 'Gaurav', 'Anuj'],
                  'Age':[27, 24, 22, 32],
                  'Address':['Delhi', 'Kanpur', 'Allahabad', 'Kannauj'],
                  'Qualification':['Msc', 'MA', 'MCA', 'Phd']}
          df=pd.DataFrame(data)
          df.set_index('Name')

          f=df.loc[['Jai','Gaurav'],['Address','Age']]
          print(f)

Still not working?

标签: pythonpython-3.xpandasdataframenumpy

解决方案


df.set_index() shout be new object, try this instead df = df.set_index('Name')


推荐阅读