2021年1月24日星期日

How do I resolve this filtering challenge in data to DataFrame?

My data in code snippet below refers.

I'm stuck in trying to filter out values inside the list of dictionaries in step2.

**Desired goal:**    0      0.00\0.00  1    1.765\n2.035  2      0.00\n0.00  3      1.65\n2.21  

Where 0.00 values are filtered-out because 'P' the specific key-value is not desired ie. 8.50 in this case. I only want to return values of 6.00 and default any other to 0.00 .

Step1:    data = [[{'A': 2.25, 'G': 17, 'P': 8.50, 'T': 9},    {'A': 1.63, 'G': 17, 'P': 8.50, 'T': 10}],   [{'A': 1.765, 'G': 17, 'P': 6.00, 'T': 9},    {'A': 2.035, 'G': 17, 'P': 6.00, 'T': 10}],   [{'A': 2.33, 'G': 17, 'P': 8.50, 'T': 9},    {'A': 1.59, 'G': 17, 'P': 8.50, 'T': 10}],   [{'A': 1.65, 'G': 17, 'P': 6.00, 'T': 9},    {'A': 2.21, 'G': 17, 'P': 6.00, 'T': 10}]]  
Step2:    df = pd.Series(pd.DataFrame(data).applymap(lambda x:x['C']).astype('str').values.tolist()).str.join("\n")  df  0      2.25\1.63  1    1.765\n2.035  2      2.33\n1.59  3      1.65\n2.21    

As you can see, values in rows (0) & (3) should read 0.00\n0.00 as desired.

Help is very much appreciated.

https://stackoverflow.com/questions/65801103/how-do-i-resolve-this-filtering-challenge-in-data-to-dataframe January 20, 2021 at 07:37AM

没有评论:

发表评论