-2

  INPUT TABLEs                   OUTPUT TABLE(Table2)     
Table1.csv   | Table2.csv              Table2.csv   
Key          |   Key                Key     Flag
a            |    a                 a        1
b            |    b                 b        1
c            |    c                 c        1
f            |    d                 d        0
             |    e                 e        0
             |    f                 f        1

Table1.csv & Table2.csv has a common Key column. If Table2.csv and Table1.csv has the same key,then i need to flag in Table2.csv as 1 else 0.

[I tried this as well. It doesnt work.Nothing gets copied or flagged - https://stackoverflow.com/a/25493765/9774860]

- table1['flag']=table2.key.map(table1.key)
Unknown
  • 77
  • 3
  • 12
  • 3
    Hi. You are more likely to get help if your problem is explained clearly, and you show what you have tried already and how it didn't work. See [ask]. – khelwood May 11 '18 at 09:05
  • Please post (*edit* your question) your actual code (with variable names) rather than just a link and also explain exactly what didn't work. – Dan May 11 '18 at 09:27
  • Thanks! I am new to stackoverflow – Unknown May 11 '18 at 09:28
  • @Dan:Code - table1['flag']=table2.key.map(table1.key) . I tried this vlookup concept.This code runs without any error.But, the column flag is empty. – Unknown May 11 '18 at 09:30
  • @Unknown - no, you need to ***edit*** your question. Not post code in the comments where it is unreadable. Also, you must post the code to generate `t1` and `t2`, don't make us guess what data structure you've used. – Dan May 11 '18 at 09:34

1 Answers1

0

Impossible to answer because you haven't shown what data structure you've used but here is a simple example using lists:

>>> t1 = ['a','b','c','f']
>>> t2 = ['a','b','c','d','e','f']
>>> flag = [t2_elem in t1 for t2_elem in t2]
>>> flag
[True, True, True, False, False, True]

Or assuming a DataFrame:

import pandas as pd
df1 = pd.DataFrame(t1, columns=['Key'])
df2 = pd.DataFrame(t2, columns=['Key'])
df1['Flag']=1
df3 = pd.merge(df2, df1, on='Key, how='left')
df3.fillna(0)

outputs

   Key  Flag
0    a   1.0
1    b   1.0
2    c   1.0
3    d   0.0
4    e   0.0
5    f   1.0
Dan
  • 45,079
  • 17
  • 88
  • 157