0

I found this Pandas Dataframe - select columns with a specific value in a specific row But I couldn't figure out how to do this in iteration from user input.

no_to_search = input()
with open('jen.csv', 'rt') as f:
    reader = csv.reader(f, delimiter=',')
    for row in reader:
        if no_to_search==row
        df=pd.read_csv('jen.csv')
        for col in df.columns:
            if (df[col] == 1).any():
                print(col)

[refer image via this link 1]

I want to retrieve column names with value 1 for the input row matching.

6
  • Do you have a full list of the column names you want to retrieve? If yes: import pandas as pd df = pd.read_csv(filename, usecols=list_of_col_names) Commented Sep 18, 2019 at 9:57
  • I'm sorry, I think I misread you question. Could you give an example of what you want, e.g. an example of data, and what you would like to extract? Commented Sep 18, 2019 at 10:00
  • @ABotros could you follow up Commented Sep 18, 2019 at 10:20
  • I've updated post with sample image of data and I need to retrieve column names with value 1 corresponding for the input number Commented Sep 18, 2019 at 10:40
  • Do you want to iterate through all the rows and get all the column names that has 1 in it or look for all the columns that has the value in the entire data frame? Commented Sep 18, 2019 at 10:43

2 Answers 2

0

OK, I think I have finally fully understood your problem. If I have understood your question correctly,

  • you want to retrieve the columns where the value of a certain row matches your condition (value = 1) Example code:
>> import pandas as pd
>> df = pd.DataFrame([[1,0,1],[0,1,0],[1,1,0]], columns=["car_db","bike_db","van_db"], index=["7602102000", "7602201132", "7622315645"])
>> df
            car_db  bike_db van_db
7602102000  1       0       1
7602201132  0       1       0
7622315645  1       1       0

>> df.to_csv("tmp.csv")

The example data is in a cvs file called "tmp.csv" and is exactly like your example. You want the name of the columns, where the phone number is 7602102000 and the value is 1:

>> df = pd.read_csv("tmp.csv", index_col=0)
>> no = 7602102000  # phone number inputed by your users
>> col_names = df.columns[(df.loc[df.index == no].values==1)[0]]
>> col_names
Index(['car_db', 'van_db'], dtype='object')
>> df[col_names]
            car_db  van_db
7602102000  1       1
7602201132  0       0
7622315645  1       0

This will print you all columns where the condition is met.

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

3 Comments

There are four columns namely ('phone no','db1','db2','db3') ...i want to let user to input 'phone no' as external input and i want to match that with 'phone no' in dataframe and return the column names with 1 as value for that specific record....
I'm getting the index of the matching outputs ..how to get the values of columns from the matching indexes?
ah, ok, so you want the whole columns where the condition is met? Just call your dataframe with df[col_names]. This will return all columns (with values) where the condition is met.
0

Here's a much more efficient way of doing this. Changed the code to suit your question. Add another condition to the lambda.

df.apply(lambda x: print(x.index[(x['phone no'] == number) & (x.isin([1])].values), axis = 1)

If you want to add a column to your data frame:

df['cols'] = df.apply(lambda x: print(x.index[(x['phone no'] == number) & (x.isin([1])].values), axis = 1)

I am sure there's a much efficient way of doing this. This should work. Let me know if it doesn't.

for i in range(len(df)): if df.columns[(df == 1).iloc[i]].notna(): print(df.columns[(df == 1).iloc[i]].values)

5 Comments

Thanks..But this displays the column names for the entire rows for the matching condition..How could i only display the column names for specific row no that i input ?
df.columns[(df == 1).iloc[row_number]].values
User input i mean....there are four columns namely ('phone no','db1','db2','db3') ...i want to let user to input 'phone no' as external input and i want to match that with 'phone no' in dataframe and return the column names with 1 as value....Hope you get it now..
df.apply(lambda x: print(x.index[(x['phone no'] == number) & (x.isin([1]))].values), axis = 1)
It prints empty arrays for other rows as well as printing required output for that row..can't i get the required array only?

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.