John W. Nesk
John W. Nesk

Reputation: 175

Pandas filter and combine

I'm new to pandas and having a hard time figuring out how to do the following

data = pd.read_csv('data.csv', sep=';', header=0, names=['a1', 'a2', 'b1', 'b2', 'c1', 'c2'])

All values are integers

I need to filter out rows where 'a1', 'b1', 'c1' are less than 5 then produce a new DataFrame with 'a', 'b', 'c' only, where

a[i] = a2[i] / a1[i]
c[i] = b2[i] / b1[i]
d[i] = c2[i] / c1[i]

Upvotes: 1

Views: 116

Answers (1)

Ami Tavory
Ami Tavory

Reputation: 76366

To filter out, you can use:

data = data[(data.a1 < 5) & (data.b1 < 5) & (data.c1 < 5)]

Then you can create a DataFrame with new columns:

pd.DataFrame({
    'a': data.a2 / data.a1,
    'c': data.b2 / data.b1,
    'd': data.c2 / data.c1})

(Note that in the preceding, though, you might consider using

    'a': data.a2.astype(float) / data.a1,

(and so on), as integer division might not be what you want.)

Upvotes: 1

Related Questions