ApJdataFrames 014: Megeath2005

Title: Spitzer/IRAC Photometry of the η Chameleontis Association
Authors: S T Megeath, L Hartmann, Kevin L Luhman, and G G Fazio

Data is from this paper:


In [1]:
%pylab inline
import seaborn as sns


Populating the interactive namespace from numpy and matplotlib

In [2]:
import warnings
warnings.filterwarnings("ignore")

In [3]:
import pandas as pd

Table 1 - IRAC Photometry


In [8]:
names = ["ID","Spectral_Type","[3.6] (error)","[4.5] (error)","[5.8] (error)","[8.0] (error)","OTHER DESIGNATION"]
tbl1 = pd.read_csv("http://iopscience.iop.org/1538-4357/634/1/L113/fulltext/19839.tb1.txt", header=0,
                   na_values="\ldots", names = names, sep='\t')
tbl1.head(1)


Out[8]:
ID Spectral_Type [3.6] (error) [4.5] (error) [5.8] (error) [8.0] (error) OTHER DESIGNATION
0 1 K6 7.17 (0.02) 7.20 (0.02) 7.16 (0.01) 7.11 (0.02) RECX 1

Ugh, it's one of these tables that puts the uncertainty in parentheses adjacent to the value. Looks nice in a table, but is shitty for parsing.
Luckily we have computers, functions, and for loops.


In [5]:
def strip_parentheses(col, df):
    '''
    splits single column strings of "value (error)" into two columns of value and error
    
    input:
    -string name of column to split in two
    -dataframe to apply to
    
    returns dataframe
    '''
    
    out1 = df[col].str.replace(")","").str.split(pat="(")
    df_out = out1.apply(pd.Series)
    
    # Split the string on the whitespace 
    base, sufx =  col.split(" ")
    df[base] = df_out[0].copy()
    df[base+"_e"] = df_out[1].copy()
    del df[col]
    
    return df

In [6]:
cols_to_fix = [col for col in tbl1.columns.values if "(error)" in col]
for col in cols_to_fix:
    print col
    tbl1 = strip_parentheses(col, tbl1)


[3.6] (error)
[4.5] (error)
[5.8] (error)
[8.0] (error)

In [7]:
tbl1.head()


Out[7]:
ID Spectral_Type OTHER DESIGNATION [3.6] [3.6]_e [4.5] [4.5]_e [5.8] [5.8]_e [8.0] [8.0]_e
0 1 K6 RECX 1 7.17 0.02 7.20 0.02 7.16 0.01 7.11 0.02
1 3 M3.25 RECX 3 9.27 0.02 9.21 0.02 9.09 0.05 9.15 0.01
2 4 M1.75 RECX 4 8.45 0.02 8.45 0.02 8.37 0.01 8.32 0.02
3 5 M4 RECX 5 9.59 0.03 9.50 0.03 9.37 0.01 8.89 0.01
4 6 M3 RECX 6 9.15 0.04 9.07 0.07 9.04 0.01 9.04 0.01

The end.