简体   繁体   中英

To filter rows from a list in a series in a dataframe in pandas

在此处输入图片说明

The above is a pandas dataframe.
The values in col1 are sort of keys for values in col2
eg: In row 3, "-4" in col1 corresponds to list[12,23] in col2, similarly
In row 3, "-2" in col1 corresponds to list[12] in col2.

I'm looking to filter out only +ve values in col1 and their corresponding values in col2.

I've tried multiple combinations of dict(zip(col1,col2)) to filter, but didn't work..

It would be really helpful, if someone can help me on this.

Here's one way using pd.DataFrame.apply and a generator comprehension with enumerate .

import pandas as pd
from operator import itemgetter

df = pd.DataFrame({'Member': [1, 2, 3],
                   'Col1': [[1, 2, 3], [-1, 2], [-4, -2, 2, 3]],
                   'Col2': [[[12, 23], [12], [4345]],
                            [[12, 23], [12]],
                            [[12, 23], [12], [4345], [34354]]]})

def list_filter(row):
    i = ((i, j) for i, j in enumerate(row['Col1']) if j > 0)
    idx, vals = zip(*i)
    return list(vals), list(itemgetter(*idx)(row['Col2']))

df[['Col1', 'Col2']] = df.apply(list_filter, axis=1).values.tolist()

print(df)

        Col1                      Col2  Member
0  [1, 2, 3]  [[12, 23], [12], [4345]]       1
1        [2]                      [12]       2
2     [2, 3]         [[4345], [34354]]       3

Here's an option just using a couple loops

row2=[]
col2=[]
row1=[]
col1=[]
for i in range(0,len(df.Col1)):
    for j in range(0,len(df.Col1.tolist()[i])):
        if df.Col1.tolist()[i][j] > 0:
            print i
            print j
            row2.append(df.Col2.tolist()[i][j])
            row1.append(df.Col1.tolist()[i][j])
    col1.append(row1)
    col2.append(row2)
    row1=[]
    row2=[]

df['Col1']=col1
df['Col2']=col2

print(df)

   Member      Col1                   Col2
0      1  [1, 2, 3]  [[12, 23], [12], [4345]]
1      2        [2]                    [[12]]
2      3     [2, 3]         [[4345], [34354]] 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM