Home Ask Login Register

Developers Planet

Your answer is one click away!

TheDaJon January 2017

Python Pandas get part of dataframe based on a mask

I have a data frame that looks like this:

      TTL1     TTL2      TTL3       TTL4
0     val1     val2      val3       val4
1     val1     val2      val3       val4
2     val1     val2      val3       val4
3     val1     val2      val3       val4

and a mask that looks like this:

[[ 0 0 0 0 ]
 [ 0 0 1 0 ]
 [ 0 1 0 0 ]
 [ 0 0 0 0 ]]

I want to cut the main df, by the values of the mask: if there is 1 in a row, get the row. so the output should be another dataframe that looks like this:

      TTL1     TTL2      TTL3       TTL4
1     val1     val2      val3       val4
2     val1     val2      val3       val4

I tried this line but it doesn't work:

df.loc[mask.any(axis=1)]

any ideas please?

thank you

Answers


piRSquared January 2017

try using loc with mask.any(1)

df.loc[mask.any(1)]

   TTL1  TTL2  TTL3  TTL4
1  val1  val2  val3  val4
2  val1  val2  val3  val4

setup

df = pd.DataFrame(
    {'TTL1': ['val1', 'val1', 'val1', 'val1'],
     'TTL2': ['val2', 'val2', 'val2', 'val2'],
     'TTL3': ['val3', 'val3', 'val3', 'val3'],
     'TTL4': ['val4', 'val4', 'val4', 'val4']}
)

mask = np.array([
        [0, 0, 0, 0],
        [0, 0, 1, 0],
        [0, 1, 0, 0],
        [0, 0, 0, 0],
    ])


jezrael January 2017

I think you can omit loc, only is necessary same length of array and df:

print (df[mask.any(axis=1)])
  TTL1  TTL2  TTL3  TTL4
1  val1  val2  val3  val4
2  val1  val2  val3  val4

Same as:

print (df[mask.any(1)])
  TTL1  TTL2  TTL3  TTL4
1  val1  val2  val3  val4
2  val1  val2  val3  val4

Post Status

Asked in January 2017
Viewed 3,529 times
Voted 12
Answered 2 times

Search




Leave an answer


Quote of the day: live life