Python 将数据帧的默认索引更改为列
我试图通过将新的索引列设置为响应者来将我的数据帧(stack overflow's survey of 2019)的默认索引从0-98854更改为1-101548。然后,与使用默认索引相比,我将使用此索引以更高的精度访问单个行 第一次尝试:我尝试使用Python 将数据帧的默认索引更改为列,python,pandas,Python,Pandas,我试图通过将新的索引列设置为响应者来将我的数据帧(stack overflow's survey of 2019)的默认索引从0-98854更改为1-101548。然后,与使用默认索引相比,我将使用此索引以更高的精度访问单个行 第一次尝试:我尝试使用df.reset\u index(drop=True)重置索引,但似乎不起作用 第二次尝试:我也尝试过使用df.set_index('responder',inplace=True)但它也不起作用——默认索引首先显示,我无法按标签搜索(例如df.lo
df.reset\u index(drop=True)
重置索引,但似乎不起作用
第二次尝试:我也尝试过使用df.set_index('responder',inplace=True)
但它也不起作用——默认索引首先显示,我无法按标签搜索(例如df.loc['3']
-keyrerror:'3'
)
数据帧预览:
Respondent Hobby OpenSource Country ... Dependents MilitaryUS SurveyTooLong SurveyEasy
0 1 Yes No Kenya ... Yes NaN The survey was an appropriate length Very easy
1 3 Yes Yes United Kingdom ... Yes NaN The survey was an appropriate length Somewhat easy
2 4 Yes Yes United States ... NaN NaN NaN NaN
3 5 No No United States ... No No The survey was an appropriate length Somewhat easy
4 7 Yes No South Africa ... Yes NaN The survey was an appropriate length Somewhat easy
... ... ... ... ... ... ... ... ... ...
98850 101513 Yes Yes United States ... NaN NaN NaN NaN
98851 101531 No Yes Spain ... NaN NaN NaN NaN
98852 101541 Yes Yes India ... NaN NaN NaN NaN
98853 101544 Yes No Russian Federation ... NaN NaN NaN NaN
98854 101548 Yes Yes Cambodia ... NaN NaN NaN NaN
df.set\u索引('responder',inplace=True)
应该可以工作。没有理由不应该df.loc['3']
不起作用,因为您的索引是int,而不是字符串。尝试df.loc[3]
df.set\u index('responder',inplace=True)
应该可以工作,你能提供一个没有工作的地方吗?