0

Here is my python code, Which is throwing error while executing.

def split_cell(s):
    a = s.split(".")
    b = a[1].split("::=")
    return (a[0].lower(),b[0].lower(),b[1].lower())

logic_tbl,logic_col,logic_value = split_cell(rules['logic_1'][ith_rule])
mems = logic_tbl[logic_tbl[logic_col]==logic_value]['mbr_id'].tolist()

Function split_cell is working fine, and all the columns in logic_tbl are of object datatypes.

HEre is the Traceback enter image description here

subro
  • 1,167
  • 4
  • 20
  • 32
  • 5
    Kindly include the complete error traceback. – shad0w_wa1k3r May 04 '17 at 12:13
  • 3
    Did you not get a solution from all the questions with the same title that were suggested when you asked this? – OneCricketeer May 04 '17 at 12:14
  • 2
    What are the type of `logic_tbl[0]` and `logic_tbl[1]`? Can you print the `logic_tbl` after that function call – kuro May 04 '17 at 12:17
  • and what is `logic_col` – depperm May 04 '17 at 12:18
  • 1
    I think the problem is: `logic_tbl[logic_col]` because `logic_col` is a string – depperm May 04 '17 at 12:21
  • @AshishNitinPatil updated please check – subro May 04 '17 at 12:25
  • @cricket_007, Yes I did check couple of links, Didn't get solution – subro May 04 '17 at 12:26
  • @kuro: In the split_cell function, I am returning them as strings. Should it not be like this, Please suggest! – subro May 04 '17 at 12:27
  • @depperm I din't understand! if logic_col is a string any problem because of this? Please suggest! – subro May 04 '17 at 12:28
  • 1
    `logic_col` is a string and thus can't be used as index for `logic_tbl`. Indices should always be integer in case of strings – kuro May 04 '17 at 12:30
  • @kuro https://i.stack.imgur.com/fT5Nc.png , please check this, First three lines are columns in logic_tbl, and (....) is the value the split_cell function returns, I printed other variables, that has empty pd series, you please ignore that! and the Traceback!;;;;;;; we refer like a['b'] when b is variable name in dataframe a , isn't it? please suggest! – subro May 04 '17 at 12:37
  • Possible duplicate of [Why am I seeing "TypeError: string indices must be integers"?](http://stackoverflow.com/questions/6077675/why-am-i-seeing-typeerror-string-indices-must-be-integers) – depperm May 04 '17 at 12:38
  • @depperm But it is involved json and csv, But this is in pandas dataframes, In this case how i do convert variable name to int? – subro May 04 '17 at 12:46
  • http://stackoverflow.com/questions/379906/parse-string-to-float-or-int – depperm May 04 '17 at 13:04

2 Answers2

0

Got this corrected!

Logic_tbl contains name of pandas dataframe

Logic_col contains name of column name in the pandas dataframe

logic_value contains value of the rows in the logic_col variable in logic_tbl dataframe.

mems = logic_tbl[logic_tbl[logic_col]==logic_value]['mbr_id'].tolist()

I was trying like above, But python treating logic_tbl as string, not doing any pandas dataframe level operations.

So, I had created a dictionary like this 
dt_dict={}
dt_dict['a_med_clm_diag'] = a_med_clm_diag

And modified my code as below,

mems = dt_dict[logic_tbl][dt_dict[logic_tbl][logic_col]==logic_value]['mbr_id'].tolist()

This is working as expected. I come to this idea when i wrote like,

mems = logic_tbl[logic_tbl[logic_col]==logic_value,'mbr_id']

And this throwed message like,"'logic_tbl' is a string Nothing to filter".

subro
  • 1,167
  • 4
  • 20
  • 32
-1

Try writing that last statement like below code:

filt = numpy.array[a==logic_value for a in logic_col]
mems = [i for indx,i in enumerate(logic_col) if filt[indx] == True]

Does this work?

Aman
  • 49
  • 8
  • If that does not work... then please try this too: filt = numpy.array[a==logic_value for a in logic_col]; mems = [i for indx,i in enumerate(logic_col) if filt[indx] == True] – Aman May 04 '17 at 12:35
  • logic_col is a variable name in pandas dataframe and logic_value is the value for one of the rows in logic_col, These two never be equals. – subro May 04 '17 at 12:49
  • Shouldn't logic_col be a numpy array – Aman May 04 '17 at 13:19