Pregunta

I have a dataframe consisting of multiple .csv files. I want to report a value "feed start" based on a value in a column in the dataframes. However, some of the dataframes are missing that column header ["FC.PV [EFT (h)"]. I just want to skip the members of the dataframe that don't have the header ["FC.PV [EFT (h)"].

shorts = ['F:/DASGIP/CSV files/Run Data/' + str(i) + '.csv' for i in files]
ferms = [pd.read_csv(s) for s in shorts]

for i in range(len(files)):    
        FStartVs =  ferms[i][["FC.PV [EFT (h)]"] > 0.0]["EFT (h)"].min()
        print "Feed Start C", files[i], "=", FStartVs[i], "EFT (h)"

I remember seeing a simple statement that allows you to skip a column in a dataframe if it is missing, but can't find it. Any help would be greatly appreciated.

¿Fue útil?

Solución

Getting rid of the DataFramess in ferms when they don't have ["FC.PV [EFT (h)"] column? Add this line after then 2nd line.

ferms=[DF for DF in ferms if '["FC.PV [EFT (h)"]' in DF.columns]

Otros consejos

You can test if a dataframe has a column:

if "FC.PV [EFT (h)" in df.columns:
    # do something

So you can skip those that don't have the column

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top