2 回答
TA贡献1871条经验 获得超8个赞
如果您不希望使用索引,则可以通过诸如的唯一列进行设置Id。
import pandas as pd
d = {'Id':[1,2,3,4,5], 'Name':['Ram','Hari','Gita','Sita','Shyam'],'Gender':['Male','Male','Female','Female','Male'],'Math':[98,30,60,50,80],'Science':[92,40,65,45,88],'English':[80,23,77,55,82]}
df = pd.DataFrame(data=d)
df.set_index('Id', inplace=True)
print (df)
输出:
Name Gender Math Science English
Id
1 Ram Male 98 92 80
2 Hari Male 30 40 23
3 Gita Female 60 65 77
4 Sita Female 50 45 55
5 Shyam Male 80 88 82
TA贡献2011条经验 获得超2个赞
尝试直接创建DataFrame而不是传递“ d”
df = pd.DataFrame({'Id': [1, 4, 7, 10], etc...})
然后使用set_index来修复您的ID:
df.set_index('Id')
添加回答
举报