0

I have the following dataframe (df):

Col_A Col_B Col_C
123 A 1Q
124 B L1
125 B QW
126 A E2

If the user selects a particular column and the value the entire row should be saved as a new dataframe. For example, if the user selects Col_A 123 the output should be the following:

Col_A Col_B Col_C
123 A 1Q

If the user selects Col_B: B and Col_A: 125 then the output should e the following:

Col_A Col_B Col_C
125 B QW

If the user selects Col_B: B then the output shoud be the following:

Col_A Col_B Col_C
124 B L1
125 B QW

How do I do that?

What I tried so far?


import pandas as pd
import numpy as np
df= pd.read_csv('Table.csv')
print('Enter the value as 99 if the Col_A or Col_B is not known')
Col_A_value= (input(str("Enter tag of your interest separated by ',': ")).split(","))
Col_B_value= (input(str("Enter iris_image_id of your interest separated by ',': ")).split(","))
input_table = []
for i,j in itertools.product(Col_A_value, Col_B_value):
    if len(i) > 0 and len(j)==0:
        input_table.append(df[df['Col_A'] == i])
    elif i != '99' and len(j)> 0:
        input_table.append(df[df['Col_A'] == i]) 

The above script does not extract a particular data if Col_A and Col_B are specified. If I specify Col_A = 124 and Col_B = B it results everything in col_B.

Desired output:

Col_A Col_B Col_C
124 B L1
3
  • Could you clarify if I am following this correctly? You nest loop both user lists but check if a has a value and b is empty to check only on a. Otherwise if a is known and a value for b is entered save row if a matches. To me the logic is really weird, why do you never check the value of B, where is the 99 case handled. I would personally approach it as get A value, get b value, check if 99 or empty so ignore, else filter to append. Commented Jan 5, 2022 at 15:47
  • Well, you are referencing "Col_A" in both the "if" and "elif" calls. Probably need to change the second "Col_A" to "Col_B". Commented Jan 5, 2022 at 15:50
  • This question on panda data frame filters might be helpful. Commented Jan 5, 2022 at 16:04

2 Answers 2

1

you can try this :

import pandas as pd
d = {'col1': [1, 2], 'col2': [3, 4], 'col3': [3, 4]}
df = pd.DataFrame(data=d)
print(df)
print(df[(df['col1']==1) & (df['col2']==3)])

Result : enter image description here

----------- OR --------

Try to use the pandas function where (link to pandas.where tuto), It is better to use function than loops

Sign up to request clarification or add additional context in comments.

Comments

0
# Importing Libraries
import pandas as pd

# Importing dataframe
df = pd.read_csv('mydata.csv')


# Col A Value input as int
first_input = int(input('Enter Col A Value'))

# Col B value input as string
second_input = str(input('Enter Col B Value'))

# checking both column values with == sign 
df[(df['Col_A'] == first_input) & (df['Col_B']== second_input)]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.