Pandas - 如何从数据帧中获取索引值

时间:2017-05-21 09:32:23

标签: python pandas dataframe

我有一个形式为

的pandas数据框
 Start Date          End Date             President       Party
0      04 March 1921    02 August 1923      Warren G Harding  Republican
1     03 August 1923     04 March 1929       Calvin Coolidge  Republican
2      05 March 1929     04 March 1933        Herbert Hoover  Republican
3      05 March 1933     12 April 1945  Franklin D Roosevelt  Democratic
4      13 April 1945   20 January 1953        Harry S Truman  Democratic
5    21 January 1953   20 January 1961   Dwight D Eisenhower  Republican
6    21 January 1961  22 November 1963        John F Kennedy  Democratic
7   23 November 1963   20 January 1969       Lydon B Johnson  Democratic
8    21 January 1969    09 August 1974         Richard Nixon  Republican
9     10 August 1974   20 January 1977           Gerald Ford  Republican
10   21 January 1977   20 January 1981          Jimmy Carter  Democratic
11   21 January 1981   20 January 1989         Ronald Reagan  Republican
12   21 January 1989   20 January 1993       George H W Bush  Republican
13   21 January 1993   20 January 2001          Bill Clinton  Democratic
14   21 January 2001   20 January 2009         George W Bush  Republican
15   21 January 2009   20 January 2017          Barack Obama  Democratic
16   21 January 2017       20 May 2017          Donald Trump  Republican

我想提取Party = Republican的索引值并将它们存储在列表中。 是否有熊猫功能可以快速完成这项工作?

1 个答案:

答案 0 :(得分:1)

df.index[df.Party == 'Republican`]

如果需要,您可以在结果上调用.tolist()