熊猫:使用一系列字符串从数据框中选择行

时间:2020-04-20 02:23:59

标签: python string pandas dataframe series

我有一个系列中的姓氏列表:

print(s)

Order
1       Surname01
2       Surname02
3       Surname03
4       Surname04
5       Surname05
          ...    
496     Surname06
497     Surname07
498     Surname08
499     Surname09
500     Surname10

我在数据框df中列出了姓名,地址,电话号码等表格:

           Unnamed: 0  Unnamed: 0.1  ...           Phone      Unnamed: 7
Last Name                            ...                                
Surname01           0           NaN  ...             NaN             NaN
Surname02           1           NaN  ...             NaN             NaN
Surname11           2           NaN  ...             NaN             NaN
Surname12           3           NaN  ...  (123) 456-7890  (123) 456-7890
Surname05           4           NaN  ...  (123) 456-7890  (123) 456-7890
...               ...           ...  ...             ...             ...
Surname13          94           NaN  ...  (123) 456-7890  (123) 456-7890
Surname14          95           NaN  ...  (123) 456-7890  (123) 456-7890
Surname15          96           NaN  ...             NaN             NaN
Surname16          97           NaN  ...             NaN             NaN
Surname17          98           NaN  ...             NaN             NaN

我想从df创建一个只有s中的姓氏的新表。 我已经读了很多页,但无法弄清楚。这是我的位置:

df = df.loc[s]

但是我收到KeyError:“姓氏”

1 个答案:

答案 0 :(得分:0)

尝试df.loc[df.index.isin(s), :]

示例

s = pd.Series(['a', 'b', 'c', 'd'])

df = pd.DataFrame(index=['a', 'f', 'd'], 
                  data={'col1': [10, 11, 12], 'col2': [1, 2, 3]})

df.loc[df.index.isin(s), :]

   col1  col2
a    10     1
d    12     3