Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using if statements to filter data?

Lets say I have an excel document with the following format. I'm reading said excel doc with pandas and plotting data using matplotlib and numpy. Everything is great!

Buttttt..... I wan't more constraints. Now I want to constrain my data so that I can sort for only specific zenith angles and azimuth angles. More specifically: I only want zenith when it is between 30 and 90, and I only want azimuth when it is between 30 and 330

Air Quality Data
Azimuth Zenith    Ozone Amount
230    50         12   
0      81         10    
70     35         7
110    90         17
270    45         23
330    45         13
345    47         6
175    82         7
220    7          8

This is an example of the sort of constraint I'm looking for.

 Air Quality Data
Azimuth Zenith    Ozone Amount
230    50         12   
70     35         7
110    90         17
270    45         23
330    45         13
175    82         7

The following is my code:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import datetime

P_file = file1
out_file = file2
out_file2 = file3

data = pd.read_csv(file1,header=None,sep=' ')
df=pd.DataFrame(data=data)
df.to_csv(file2,sep=',',header = [19 headers. The three  that matter for this question are 'DateTime', 'Zenith', 'Azimuth', and 'Ozone Amount'.]
df=pd.read_csv(file2,header='infer')
mask = df[df['DateTime'].str.contains('20141201')] ## In this line I'm sorting for anything containing the locator for the given day.
mask.to_csv(file2) ##I'm now updating file 2 so that it only has the data I want sorted for.
data2 = pd.read_csv(file2,header='infer')
df2=pd.DataFrame(data=data2)

def tojuliandate(date):
   return.... ##give a function that changes normal date of format %Y%m%dT%H%M%SZ to julian date format of %y%j
def timeofday(date):
    changes %Y%m%dT%H%M%SZ to %H%M%S for more narrow views of data

df2['Time of Day'] = df2['DateTime'].apply(timeofday)
df2.to_csv(file2) ##adds a column for "timeofday" to the file

So basically at this point this is all the code that goes into making the csv I want to sort. How would I go about sorting

'Zenith' and 'Azimuth' 

If they met the criteria I specified above?

I know that I will need if statements to do this. I tried something like this but it didn't work and I was looking for a bit of help:

like image 402
haramassive Avatar asked Dec 09 '25 17:12

haramassive


1 Answers

df[(df["Zenith"]>30) & (df["Zenith"]<90) & (df["Azimuth"]>30) & (df["Azimuth"]<330)]

Basically a duplicate of Efficient way to apply multiple filters to pandas DataFrame or Series

like image 108
Ma.Na Avatar answered Dec 12 '25 05:12

Ma.Na



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!