operations on column length

2024/11/16 3:37:06

Firstly, sorry if I have used the wrong language to explain what I'm operating on, I'm pretty new to python and still far from being knowledgeable about it.

I'm currently trying to do operations on the length of a column of data however I'm running into a few problems. These columns are from a .fit file from the 7th sdss data dump. When I run the code each value for x1, x2, x3 is printed according to the boundary conditions.

x1 = o3[(03 >= y1)]
print len(x1)
x2 = o3[(o3 < y2) & (o3 < y1)]
print len(x2)
x3 = o3[(o3 < y1) & (o3 >= y2)]
print len(x3)
xtotal = len(x1) + len(x2) + len(x3)
print xtotal

From this point I obtain values for x1, x2, x3 of around 70000, 90000 and 30000 with a total of around 190000. I would like to include a line to calculate what percentage these are of the total value however I can't seem to get this to work.

Having tried.

x1percentage = (xtotal/x1)*100
print x1percentage

This returns the elements operated on not the lengths but when i try to define the length as just a value it returns an answer of 0.

Any help would be much appreciated.

Answer

You are probably making an error with integer math, but your statement

x1percentage = (xtotal/x1)*100

doesn't really make sense. xtotal is an integer and x1 is an array of values, len(x1) is an integer equal to the size of x1, and regardless to get a percentage you would want x1/xtotal.

If you tried len(x1)/xtotal, you would get zero as you mentioned, because python would cast both variables as integers and use integer math, which the result is zero.

The solution cast one or both values as floats to force floating point division by

x1percentage = (xtotal / float(x1)) * 100
https://en.xdnf.cn/q/119579.html

Related Q&A

Python: Parse one string into multiple variables?

I am pretty sure that there is a function for this, but I been searching for a while, so decided to simply ask SO instead.I am writing a Python script that parses and analyzes text messages from an inp…

How do I pull multiple values from html page using python?

Im performing some data analysis for my own knowledge from nhl spread/betting odds information. Im able to pull some information, but Not the entire data set. I want to pull the list of games and the a…

Creating h5 file for storing a dataset to train super resolution GAN

I am trying to create a h5 file for storing a dataset for training a super resolution GAN. Where each training pair would be a Low resolution and a High resolution image. The dataset will contain the d…

How to resolve wide_to_long error in pandas

I have following dataframeAnd I want to convert it into the following format:-To do so I have used the following code snippet:-df = pd.wide_to_long(df, stubnames=[manufacturing_unit_,outlet_,inventory,…

Odoo 10: enter value in Many2one field dynamically

I added in my models.py :commercial_group = fields.Many2one("simcard.simcard")and in my views.xml :<field name="commercial_group" widget="selection"/>And then i am t…

How to erode this thresholded image using OpenCV

I am trying to first remove the captcha numbers by thresholding and then eroding it ,to get slim continuous lines to get better output. Problem:the eroded image is not continuous as u can see Original …

Searching for only the first value in an array in a csv file

So i am creating a account login system which searches a database for a username (and its relevant password) and, if found, will log the user on.This is what the csv file currently looks like[dom, ente…

how to write a single row cell by cell and fill it in csv file

I have a CSV file that only has column headers:cat mycsv.csvcol_1@@@col_2@@@col_3@@@col_3I have to fill a single row with None values in each cell of the CSV file. Can someone suggest me the best-optim…

Greedy String Tiling in Python

I am trying to learn greedy string tiling in algorithmI have two lists as follows:a=[a,b,c,d,e,f] b=[d,e,a,b,c,f]i would like to retrieve c=[a,b,c,d,e]Another example would be a = [1,2,3,4,5,6,7,8,9,1,…

Python - efficient way to create 20 variables?

I need to create 20 variables in Python. That variables are all needed, they should initially be empty strings and the empty strings will later be replaced with other strings. I cann not create the var…