THN
THN

Reputation: 111

How to identify integers in a pandas dataframe containing both floats and integers

I am writing some code to identify possible lattice parameters from diffraction peaks. In order to select the correct parameters I wish to determine which combinations of integers are either wholly even or odd. To do this I have used the following function guess_table.insert(5,'FCC_condition',(guess_table[0]%2+guess_table[1]%2+guess_table[2]%2)/3) which gives the output :

enter image description here

Here I want my FCC_condition column to remove any values that arent either 0 or 1 by using guess_table2=guess_table.drop(guess_table[(guess_table['FCC_condition']<1)|(guess_table['FCC_condition']!=0)].index).sort_values('Sums_sqd',ascending=1).reset_index(). However, the problem is that I need to either specify my FCC_condition values as integers or floats, meaning I cant select wholely even or odd combinations of integers in this way. Therefore, is there a way that I can almost dynamically assign a type within a function to avoid this problem?

Upvotes: 0

Views: 126

Answers (1)

orlp
orlp

Reputation: 117681

Keep the whole column as a float. You can test if a value x is (close enough) to an integer by testing (x + eps) % 1 < 2*eps for some small value of eps. E.g.

>>> a = np.array([0, 1, .6667, .3333, 1, .999])
>>> eps = 1e-6
>>> (a + eps) % 1 < 2*eps
array([ True,  True, False, False,  True, False])

Upvotes: 1

Related Questions