lundi 27 mars 2017

How to update the column values and it's corresponding index based on a values of the column?

In the pandas Dataframe of following structure:

mcve_data =

alfa   alfa_id     beta    beta_id
a,c    7           c,de    8
c,d    7           d,f     9
l,mnk  8           c,d     9
j,k    8           d,e     9
tk,l   8           n,k     11

  • I want to run a for-loop in each line reading the values from key (alfa and beta) and key_index (alfa_index, beta_index).
  • If the values in the key is more than 3 in length or if any values are more than 1 character long. I want both the key-values and key-index to convert to period ..

Final expected output:

alfa   alfa_id     beta    beta_id
a,c    7           .      .
c,d    7           d,f     9
.      .           c,d     9
j,k    8           d,e     9
.      .           n,k     11

I wanted to write a function something like (but it hasn't worked properly):

def check_and_convert(mcve_data):
    labels = (l, l + id) for l in mcve_data.columns.values

    def convert(lines):
        for l,id in labels:
            if len(l) > 3:
                l = '.'
                id = '.'
        return l, id

        write this back to the file.

Any suggestions,

Aucun commentaire:

Enregistrer un commentaire