-2

I have a pandas dataframe with columns and rows. Now I want to create another column which will be a concatenation of two strings and a column from the dataframe. so the way it would work is i have string one (see the below dictionary)+ colx (from dataframe) + string two

stringList = {
     'one': """ AC:A000 AMI:NO CM:B C:YES CL:CPN:'#US3L+""",
     'two': """ FRQ:4  NOT:1 PX:C PXND:1E-6:DOWN RDTE:MAT RP:1 SET:0WW XD:NO """
}

i tried to create a function but I think this is not working as I want. I want this to be a function so i can call it in another function.

def fun(final):

    for i in dm:
        c = stringList['one'] + str(dm[i]) + stringList['two']
        final.append(c)

Please help with this as I am stuck with this problem for now.

Required Output:

str1                                      |QM     |str2          |output
 AC:A000 AMI:NO CM:B C:YES CL:CPN:'#US3L+ |0.0125 | RQ:4  NOT:1 PX:C PXND:1E-6:DOWN RDTE:MAT RP:1 SET:0WW XD:NO|    AC:A000 AMI:NO CM:B C:YES CL:CPN:'#US3L+0.0125RQ:4  NOT:1 PX:C PXND:1E-6:DOWN RDTE:MAT RP:1 SET:0WW XD:NO

AC:A000 AMI:NO CM:B C:YES CL:CPN:'#US3L+ 0.016 RQ:4 NOT:1 PX:C PXND:1E-

Hope this helps explain. I know it is not a very good representation but I have this problem which is critical to solve THanks

5
  • 1
    What is the output you get, and what do you expect? Commented Jun 29, 2018 at 23:09
  • 1
    There's no such thing as a python dataframe. Are we talking pandas here? Commented Jun 29, 2018 at 23:11
  • @cricket_007 I have edited the question to include output Commented Jun 29, 2018 at 23:26
  • @mypetlion my bad. meant to say pandas Commented Jun 29, 2018 at 23:26
  • Can you add dm and how you are calling fun? Also loops over dataframes are bad. See stackoverflow.com/a/40045819/2308683 Commented Jun 30, 2018 at 0:50

1 Answer 1

1

After looking at your output, I realized that you want to combine three columns str1, QM, and str2. I am assuming here that str1 and str2 have dtype str and QM has dtype float. You can use the following code to get the output column as below

df["output"] = df["str1"] + df["QM"].astype(str) + df["str2"]

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

Comments

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.