2

This is a powerful simplification of my earlier problem :)

There are 4 lists (In my case the values are integers):

e00 = [AB, CA]
e01 = [BD, FK]
e10 = [AC, DF]
e11 = [DE, FB]

next:

a = input('Write a binary number. \n')

    b = []
    for i in range(len(a)-1):
        c = a[i:i+2]
        b.append(c)

For example, for string 00101, I get:

b = ['00', '01', '10', '01']

Stages:

  1. I choose the first two values in list 'b', ie '00' and '01'
  2. For this reason, I will use lists e00 and e01
  3. I compare the above lists. My code just for this case:

    a00 = pd.DataFrame({'A':e00})
    a01 = pd.DataFrame({'B':e01})
    a10 = pd.DataFrame({'C':e10})
    a11 = pd.DataFrame({'D':e11})
    
        b = []
        for i in range(len(a)-1):
            c = a[i:i+2]
            b.append(c)
    
    if b[0]=='00':
       if b[1]=='01':
    
           m001 = a00.assign(x=a00.A.str[-1]).merge(a01.assign(x=a01.B.str[0], B=a01.B.str[1:]))
    
           v = (m001['A'] + m001['B'])
           v.str.len() == v.apply(set).str.len()
           c=v[v.str.len() == v.apply(set).str.len()].tolist()
    

Which gives:

c = [ABD]
  1. I choose the third value from the list 'b', ie '10'
  2. For this reason I will use c and e10
  3. I compare the above lists.
  4. I get c = [ABDF]
  5. I choose the last fourth value in the list 'b', ie '01'
  6. For this reason I will use c and e01
  7. I compare the above lists.
  8. Finally, I get c = [ABDFK]
  9. End

I hope now the problem will be brighter :)

14
  • 1
    minimal reproducible example if you can? Along with expected output, if you can reduce the explanation. Thanks! Commented Nov 19, 2017 at 18:40
  • @ cᴏʟᴅsᴘᴇᴇᴅ This fragment above works alone. just yet of course import pandas as pd :) Commented Nov 19, 2017 at 18:47
  • @cᴏʟᴅsᴘᴇᴇᴅ and only after typing '001'. Commented Nov 19, 2017 at 18:48
  • I believe coldspeed is asking for more readability, in particular, shorten the explanation and break into 2 or 3 smaller paragraphs instead of just one big one. And often, just showing a simple example of desired output is easier to understand than a long explanation. Commented Nov 19, 2017 at 18:50
  • @JohnE just do not know how it's easier to explain eh :) Commented Nov 19, 2017 at 18:53

1 Answer 1

2

If I understood you clearly, you're looking for a way to dynamically access aXX so you can then use them to perform your computation.

What you need is a dictionary.

dct = {'00' : a00, '01' : a01, '10' : a10, '11' : a11}    
c = dct[b[0]]

for i in b[1:]:
    d = dct[i]
    newcols = {'x' : d.iloc[:, 0].str[0], d.columns[0] : d.iloc[:, 0].str[1:]}
    m = c.assign(x=c.iloc[:, 0].str[-1]).merge(d.assign(**newcols))

    ... # rest of your code

You can use b's elements to index into dct and extract the dataframes you need for that iteration.

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

4 Comments

you are great :) Everything works great on the above case. But I'm trying to modify it now for all frames a00, a01, a10, a11, because in each frame I fixed a different column name. And in 'm' only A and B are used.
@TomaszPrzemski You can refer to the first column with df.iloc[:, 0].
@TomaszPrzemski I see the problem of dynamic columns is a bigger issue than I thought. So I'm initialising a dictionary of new columns and passing that to d after. It's easier. Take a look at my latest edit, I hope it helps.
However, I will still have to sit down for the night :) Since probably 'dict' blocks certain cases like "000". And the question of using longer binary strings is open. For now it works only on three digits :) But and anyway you helped me so much!

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.