0

I'm trying to select 10 cases into one new data frame with pandas, but I getting one problem. I'm using this code:

    import pandas as pd
    import csv
    import geopy
    import numpy as np
    import geopandas as gpd

    new_df = df.loc(axis=0)[df['cod'] == 569852, 478521, 
    159632, 458216, 521562, 258632, 584526, 596325, 596325, 512584]

I'm getting the following error

TypeError: 'Series' objects are mutable, thus they cannot be hashed

I already tried with

new_df = df.loc(axis=0)[df['cod'] == '569852', '478521', 
        '159632', '458216', '521562', '258632', '584526', '596325', '596325', '512584']

But the error is the same. What I'm doing wrong?

Heavy Hammer
  • 291
  • 3
  • 11

1 Answers1

3

You can use .isin() to compare a series against an iterable. So in your case, you can have

new_df = df[df['cod'].isin([569852, 478521, 159632, 458216, 521562, 258632, 584526, 596325, 596325, 512584])
asongtoruin
  • 9,794
  • 3
  • 36
  • 47
  • After df.loc[df['cod'].isin([569852, 478521, 159632, 458216, 521562, 258632, 584526, 596325, 596325, 512584]), I would add .copy() just in case you are slicing new_df. It will avoid a warning message to my knowledge. – Andrew Hamel Aug 08 '19 at 14:28