messier
messier

Reputation: 65

How to compare two columns in csv with two values declared before in python?

the catalog there this values:

0.6881  -55.0099
0.6883  -80.3465
0.7827  -59.5199
0.8206  -54.5565
0.8418  -49.7932
0.9503  -43.6182
1.0236  -52.9165
1.0488  -50.6465
1.1068  -50.6182
1.134   -32.2499
1.1325  -45.4482
1.2633  -27.7416
1.2725  -50.8282
1.3115  -40.6049
1.3461  -30.5649
...
...

and my values declared before is ra = 59.6517601 dec = 61.5475502. How compare to show if there values ra, dec in my catalog?

Upvotes: 0

Views: 57

Answers (3)

messier
messier

Reputation: 65

thanks for help to answer my question and sorry for my delay to answer here! Well, I find a simple solution for this. I'm working with fits files.

catalogue = pd.read_csv('cat7.csv', delimiter=(','))
catalogue.columns = ['ra', 'dec']
df1 = catalogue[['ra','dec']]

ra = []
dec = []

for r in df1['ra']:
    ra.append(r)

for d in df1['dec']:
    dec.append(d)

for filename in os.listdir('6df'):
    print('reading file ' + filename)
    if filename.endswith('.fits'):
        hdulist = fits.open('6df/' + filename)
        try:
            ra1  = hdulist[2].header['ra']
            dec1 = hdulist[2].header['dec']


            for i in range(0, len(df1)):
                if (int(ra1) == int(ra[i]) and int(dec1) == int(dec[i])):
                    shutil.copy('6df/' + filename, 'type/' + filename)
                    print(filename + ' copiado')
                    break
        except:
            shutil.copy('6df/' + filename, 'SEM_RA_DEC/' + filename)

Upvotes: 1

Alireza
Alireza

Reputation: 706

Before anything, if the decimals of ra and dec are different from those of your values in csv file, use round() to fix the decimals first, then do the following action

using Pandas:

import pandas

ra = 59.6517601
dec = 61.5475502
pattern = [ra,dec]

table = pandas.read_csv('file.csv')
for ind,row in table.iterrows():
    if list(row.values)==pattern:
        print('pattern was detected at index {}'.format(ind))

and without pandas:

cnt = 0

ra = 59.6517601
dec = 61.5475502
pattern = [ra,dec]

with open('file.csv','r').read() as file:
    for row in file.split('\n'):
        x = [float(row.split(',')[0]),float(row.split(',')[1])]
        if x==pattern:
            print('pattern was detected at index {}'.format(cnt))
            break
    cnt += 1

just remember that if you come into ValueError in the second solution, it is likely because of the split behavior for text in files, where an empty element is created at last index of the list. to avoid the error, do:

file = file[:-1] after the with syntax

Upvotes: 2

karen
karen

Reputation: 812

First create the catalogue in python:

l = '0.6881 -55.0099 0.6883 -80.3465 0.7827 -59.5199 0.8206 -54.5565 0.8418 -49.7932 0.9503 -43.6182 1.0236 -52.9165 1.0488 -50.6465 1.1068 -50.6182 1.134 -32.2499 1.1325 -45.4482 1.2633 -27.7416 1.2725 -50.8282 1.3115 -40.6049 1.3461 -30.5649'

l = l.replace(' ', ',')
l = l.split(',')
l = [float(a) for a in l]

ra = 59.6517601
dec = 61.5475502

ra in l
dec in l

Both output False, so neither ra or dec are on the list. Imagine ra was in the list, then:

l.append(ra)
ra in l
True

Upvotes: 2

Related Questions