0

I'm trying to read just the value after the decimal point of a parameter calculated in my python script for a whole data set. I've read up on using math.modf, and I understand how it should work but I'm unsure how to apply that to my dataset as a whole. Ultimately I'm trying to plot a scatter graph with the values calculated.

I only need the number after the decimal point from the equation where x is the imported dataset

p = (x[:,0]-y)/z

I understand math.modf gives a result (fractional, integer) so I tried adding [0] at the end but I think that interferes when I'm trying to read certain lines from the dataset. Sorry if this is really basic I'm new to python, thanks in advance.

This is how I've inputted it so far

norm1 = np.loadtxt("dataset")
y = (numerical value)
z = (numerical value)
p = (norm1[:,0] - y)/z
decp = math.modf(p)

plt.scatter(decp, norm1[:,2])
Tetra
  • 11
  • 3
  • If u give us r code i can fix it – Luis Bote Mar 23 '21 at 11:38
  • Please provide a [mre] so it is possible to help you – Tomerikoo Mar 23 '21 at 11:38
  • Does this answer your question? [Apply function to each cell in DataFrame](https://stackoverflow.com/questions/39475978/apply-function-to-each-cell-in-dataframe) – Tomerikoo Mar 23 '21 at 11:40
  • I've edited the post to include some more of the code – Tetra Mar 23 '21 at 12:25
  • The "normal" way to do this would be with a for loop. However, is using the Pandas package an option (like in the question Tomerikoo linked)? Since you mention your data set is very large, this would almost certainly be much more efficient. – CrazyChucky Mar 23 '21 at 12:28
  • I'm very new to python so I'm currently trying to read over what Tomerikoo linked to try and actually understand it before I can try it. I find understanding what people are suggesting quite hard because I'm not particularly programming minded yet – Tetra Mar 23 '21 at 12:35
  • (My bad actually, I somehow didn't notice you're already using NumPy.) – CrazyChucky Mar 23 '21 at 17:38

2 Answers2

0

Use like that:

import math
x = 5/2
print(x)
x = math.modf(x)[0]
print(x)

output:

2.5
0.5

edit 1:

For entire dataset:

import math
x = 5/2
y = 5/2
list = []
list.append(x)
list.append(y)
print(list)


for i in range(len(list)):
    list[i] = math.modf(list[i])[0]
print(list)

output:

[2.5, 2.5]
[0.5, 0.5]
Luis Bote
  • 169
  • 2
  • 13
  • As the OP stated, the problem is not with calculating a single value, but with applying this to a dataset – Tomerikoo Mar 23 '21 at 11:39
0

Can't you handle the numbers as a string and cast it back to int?

# example input
nbrs = [45646.45646, 45646.15649, 48646.67845, 486468.15684]

def get_first_frac(n: int)->int:
    return int(str(n).split('.')[1][0])

nbrs_frac = [get_first_frac(n) for n in nbrs]
print(nbrs_frac)

result:

[4, 1, 6, 1]

edit: to apply this on a np array do the following

result = np.array(list(map(get_first_frac, x)))
Dr. Casual
  • 418
  • 3
  • 10