1

I want to take input of the column name and row name from the user and print the intersection of these as output. Python code used: here rm is a dataframe

Uid=input("Enter user id")
Iid=input("Enter item id:")
print(rm.loc[Uid][Iid]) 

here Userid is stored in column and Itemid in row

sample dataframe being used:

userID 132560 132561 132564 132572 132583 132584 \ 0 U1001 1.111111 1.111111 1.111111 1.111111 1.111111 1.111111
1 U1002 1.400000 1.400000 1.400000 1.400000 1.400000 1.400000
2 U1003 1.615385 1.615385 1.615385 1.615385 1.615385 1.615385
3 U1004 1.875000 1.875000 1.875000 1.875000 1.875000 1.875000
4 U1005 1.333333 1.333333 1.333333 1.333333 1.333333 1.333333

 132594    132608    132609    ...       135081    135082    135085  \

0 1.111111 1.111111 1.111111 ... 1.111111 1.111111 0.000000
1 1.400000 1.400000 1.400000 ... 1.400000 1.400000 1.000000
2 1.615385 1.615385 1.615385 ... 1.615385 1.615385 1.615385
3 1.875000 1.875000 1.875000 ... 1.875000 1.875000 1.875000
4 1.333333 1.333333 1.333333 ... 1.333333 1.333333 1.333333

 135086    135088    135104    135106    135108    135109      mean  

0 1.111111 1.111111 1.111111 1.111111 1.111111 1.111111 1.111111
1 1.400000 1.400000 1.400000 1.000000 1.400000 1.400000 1.400000
2 1.615385 1.615385 1.615385 1.615385 1.615385 1.615385 1.615385
3 1.875000 1.875000 1.875000 2.000000 1.875000 1.875000 1.875000
4 1.333333 1.333333 1.333333 1.333333 1.333333 1.333333 1

I even tried with rm.loc[Uid,Iid]

but,that gives me an error that that given userid is not present when it actually is. Please help me out. :(

SolitaryReaper
  • 1,823
  • 1
  • 8
  • 13
  • Something like ?`rm.loc[:,'Uid'].loc[Iid]` – BENY Nov 09 '17 at 00:23
  • are your user id and item id string or integer? If they are integers, you will have to first convert the Uid and Iid to int because input returns string – Vaishali Nov 09 '17 at 00:24
  • But,my userid has to be a string.. Does Dataframe.loc[ ][ ] not take a string,I mean as the column and row labels.Is it necessary for it be an integer? – SolitaryReaper Nov 09 '17 at 00:39
  • If you post your df.head(), it would make it more clear to understand the exact problem – Vaishali Nov 09 '17 at 00:42
  • I've added it to th question – SolitaryReaper Nov 09 '17 at 00:49
  • This same rm.loc[][] works fine when indexed with already specified column names and row names but,gives error with input – SolitaryReaper Nov 09 '17 at 00:58
  • @cᴏʟᴅsᴘᴇᴇᴅ, not an exact dupe plus both values are columns. Got more clear after df.head – Vaishali Nov 09 '17 at 01:32
  • @Ankita Das, Given that both uid and iid are columns, direct indexing won't work as it assumes the first value to be the row index. You have to use df.loc[df['userID']==Uid, Iid] – Vaishali Nov 09 '17 at 01:34
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/158558/discussion-between-ankita-das-and-vaishali). – SolitaryReaper Nov 09 '17 at 01:40

0 Answers0