1

This is how my dataframe looks:

 FULL_NAME    ARGUMENT    DEF_VALS         TYPE
 function1    f1_arg1     NAN              NoneType   
 function1    f1_arg2     NAN              NAN
 function1    f1_arg3     NAN              NAN
 function2    f2_arg1     0                int
 function3    f3_arg1     True             bool
 function3    f3_arg2     'something'      str

Here's how that data can be reproduced:

import pandas


D = {'FULL_NAME': ['function1', 'function1', 'function1', 'function2', 'function3', 'function3'], 'ARGUMENT': ['f1_arg1', 'f1_arg2', 'f1_arg3', 'f2_arg1', 'f3_arg1', 'f3_arg2'], 'DEF_VAL': [float('nan'), float('nan'), float('nan'), 0, True, 'something'], 'TYPE': ['NoneType', float('nan'), float('nan'), 'int', 'bool', 'str']}
dataframe = pandas.DataFrame(D)

Using this dataframe I need to somehow write the following info into a text file:

a1=None
a2=
a3=
function1(f1_arg1=a1, f1_arg2=a2, f1_arg3=a3)

a1=0
function2(f2_arg1=a1)

a1=True
a2='something'
function3(f3_arg1=a1, f3_arg2=a2)

The conditions for writing are the following:

a{i} should be equal to an argument default value unless the default value is NAN and its type is NAN (in this case it should be followed by the '=' sign). If the default value of the argument is NAN but the type is NoneType then a{i} must be None.

All the values in the columns 'FULL_NAME' and 'ARGUMENT' are strings.

There is also one more condition that complicates everything immensely: if the value of one of the arguments is equal to some special value (special_value) then it should not be included into the function signature. For example, if we have function g(W, r, dim, fix=False, r=0) where 'W' is the special argument, then what should be written to the text file for this function is this:

a1=
a2=
a3=False
a4=0
g(r=a1, dim=a2, fix=a3, r=a4)

Where 'a1' corresponds to 'r', 'a2' corresponds to 'dim' etc and 'W' is ignored.

How could I get that output by using pandas? Is it possible at all?

EDIT:

In more simple terms, having this dataframe:

 FULL_NAME    ARGUMENT    DEF_VALS         TYPE
 function1    f1_arg1     NAN              NoneType   
 function1    f1_arg2     NAN              NAN
 function1    f1_arg3     NAN              NAN
 function2    f2_arg1     0                int
 function3    f3_arg1     True             bool
 function3    f3_arg2     'something'      str

and taking into account the above-mentioned conditions I would like to make it look like this:

function                                       args
function1(f1_arg1=a1, f1_arg2=a2, f1_arg3=a3)  ['a1=None', 'a2=', 'a3='] 
function2(f2_arg1=a1)                          ['a1=0']
function3(f3_arg1=a1, f3_arg2=a2)              ['a1=True', 'a2=something']
4
  • Sorry for such a long question... Commented Sep 9, 2017 at 9:34
  • It would be helpful if you provided a minimal, complete and verifiable example. It seems like you're describing more of what you need written for you which is not how the SO community works. Please edit your question to include only specific parts you need help on after making an attempt. Commented Sep 9, 2017 at 10:01
  • @AndrewL I have no idea how to describe this in any other way. I'm describing exactly what I need help on trying to make it as short and clear as I can. But I will edit it to make it more clear. Commented Sep 9, 2017 at 10:19
  • @AndrewL now the dataframe can be reproduced by using the dictionary I've edited into the question. Commented Sep 9, 2017 at 10:50

1 Answer 1

1

This can be achieve with some string manipulation,groupby cumcount and bit of apply i.e

df['args']='a'+(df.groupby('FULL_NAME').cumcount()+1).astype(str)

df['ARGUMENT'] =   df['ARGUMENT']+ '=' + df['args']

df['args'] += '='

df['args'] = df.apply(lambda x: x['args']+'NONE' if x['TYPE'] == 'NoneType' else x['args'] 
                        if pd.isnull(x['TYPE']) else x['args']+str(x['DEF_VAL']),1   ) 

ndf = pd.concat([pd.DataFrame(df.groupby('FULL_NAME')['ARGUMENT'].apply(tuple)),
       pd.DataFrame(df.groupby('FULL_NAME')['args'].apply(list))],1)

ndf['function'] = (ndf.reset_index()['FULL_NAME'] + ndf.reset_index()['ARGUMENT'].apply(str)).tolist()

ndf = ndf.reset_index(drop=True).drop('ARGUMENT',1)

ndf['function'].replace(["'",",\)"],["",")"],regex=True,inplace=True)

Output:

                       args                                       function
0      [a1=NONE, a2=, a3=]  function1(f1_arg1=a1, f1_arg2=a2, f1_arg3=a3)
1                   [a1=0]                          function2(f2_arg1=a1)
2  [a1=True, a2=something]              function3(f3_arg1=a1, f3_arg2=a2)

Hope it helps.

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

8 Comments

I'm sooo grateful, thank you ever so much! Not only for the answer but for the lesson on how to do it properly. I will have to integrate it into my code and see how it works with the actual data. As for the 'special condition', now it seems obvious how to handle it.
there're still two problems...the first one occurs when a function has only one argument, its signature looks like this ('f2_arg1=a1',), but is it possible to somehow make it (f2_arg1=a1). Namely, without the comma at the end (which comes from the tuple) and without quotes? And the second problem is...well probably it's difficult, but is it possible to take the special argument case into account?
By the special case I mean this: when a method has the self argument, it must be ignored in the signature and in a{i}, that's, i should nevertheless start from 1, not from 2. But seems like it's not easy...
I don`t know much about the special case but for your first question you can use regex since all of them are strings. Hope it helps
Aah this is how it's done. Thank you! But as for the special case, you mean you don't know how to handle this or you don't fully understand what I mean? I can write about it more....but seems like I've asked you so many questions already....Sorry.
|

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.