Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Add a column in a numpy_array Python

I'm using a numpy array with Python and I would like to know how I can add a new column at the end of my array?

I have an array with N rows and I calculate for each row a new value which is named X. I would like, for each row, to add this new value in a new column.

My script is (the interesting part is at the end of my script) :

#!/usr/bin/python
# coding: utf-8
from astropy.io import fits
import numpy as np
#import matplotlib.pyplot as plt
import math
 #########################################
 # Fichier contenant la liste des champs #
 #########################################
with open("liste_essai.txt", "r") as f :
 fichier_entier = f.read()
 files = fichier_entier.split("\n")
for fichier in files :
 with open(fichier, 'r') :
 reading = fits.open(fichier) # Ouverture du fichier à l'aide d'astropy
 tbdata = reading[1].data # Lecture des données fits
 #######################################################
 # Application du tri en fonction de divers paramètres #
 #######################################################
 #mask1 = tbdata['CHI'] < 1.0 # Création d'un masque pour la condition CHI
 #tbdata_temp1 = tbdata[mask1]
 #print "Tri effectué sur CHI"
 #mask2 = tbdata_temp1['PROB'] > 0.01 # Création d'un second masque sur la condition PROB
 #tbdata_temp2 = tbdata_temp1[mask2]
 #print "Tri effectué sur PROB"
 #mask3 = tbdata_temp2['SHARP'] > -0.4 # Création d'un 3e masque sur la condition SHARP (1/2)
 #tbdata_temp3 = tbdata_temp2[mask3]
 #mask4 = tbdata_temp3['SHARP'] < 0.1 # Création d'un 4e masque sur la condition SHARP (2/2)
 #tbdata_final = tbdata_temp3[mask4]
 #print "Création de la nouvelle table finale"
 #print tbdata_final # Affichage de la table après toutes les conditions
 #fig = plt.figure()
 #plt.plot(tbdata_final['G'] - tbdata_final['R'], tbdata_final['G'], '.')
 #plt.title('Diagramme Couleur-Magnitude')
 #plt.xlabel('(g-r)')
 #plt.ylabel('g')
 #plt.xlim(-2,2)
 #plt.ylim(15,26)
 #plt.gca().invert_yaxis()
 #plt.show()
 #fig.savefig()
 #print "Création du Diagramme"
 #hdu = fits.BinTableHDU(data=tbdata_final)
 #hdu.writeto('{}_{}'.format(fichier,'traité')) # Ecriture du résultat obtenu dans un nouveau fichier fits
 #print "Ecriture du nouveau fichier traité"
 #################################################
 # Détermination des valeurs extremales du champ #
 #################################################
 RA_max = np.max(tbdata['RA'])
 RA_min = np.min(tbdata['RA'])
 #print "RA_max vaut : " + str(RA_max)
 #print "RA_min vaut : " + str(RA_min)
 DEC_max = np.max(tbdata['DEC'])
 DEC_min = np.min(tbdata['DEC'])
 #print "DEC_max vaut : " + str(DEC_max)
 #print "DEC_min vaut : " + str(DEC_min)
 #########################################
 # Calcul de la valeur centrale du champ #
 #########################################
 RA_central = (RA_max + RA_min)/2.
 DEC_central = (DEC_max + DEC_min)/2.
 #print "RA_central vaut : " + str(RA_central)
 #print "DEC_central vaut : " + str(DEC_central)
 print " "
 print " ######################################### "
 ##############################
 # Détermination de X et de Y #
 ##############################
 i = 0
 N = len(tbdata)
 for i in range(0,N) :
 print "Valeur de RA à la ligne " + str(i) + " est : " + str(tbdata['RA'][i])
 print "Valeur de RA_moyen est : " + str(RA_central)
 print "Valeur de DEC_moyen est : " + str(DEC_central)
 X = (tbdata['RA'][i] - RA_central)*math.cos(DEC_central)
 Add_column = np.vstack(tbdata, X) # ==> ????
 print "La valeur de X est : " + str(X)
 print " "

I tried something but I'm not sure that's working.

And I've a second question if it's possible. In the plot part, I would like to save my plot for each file but with the name of each file. I think that I need to write something like :

plt.savefig('graph',"{}_{}".format(fichier,png))

Answer*

Draft saved
Draft discarded
Cancel

lang-py

AltStyle によって変換されたページ (->オリジナル) /