Home > Mobile >  Extract 2 colums out of a Dataframe
Extract 2 colums out of a Dataframe

Time:01-29

I have a very simple Problem I guess.

I have loaded an csv file into python of the form:

Date Time
18.07.2018 12:00 AM
18.07.2018 12:30 AM
... ...
19.07.2018 12:00 AM
19.07.2018 12:30 AM
... ...

I basically just want to extract all rows with the Date 18.07.2018 and the single one from 19.07.2018 at 12:00 AM to calculate some statistical measures from the Data.

My current Code (Klimadaten is the Name of the Dataframe):

Klimadaten = pd.read_csv ("Klimadaten_18-20-July.csv")
Day_1 = Klimadaten[Klimadaten.Date == "18.07.2018"]

I guess it could be solved with something like an if statment?

I have just a very basic knowledge of python but im willing to learn the necessary steps. I'm currently doing my Bachelorthesis with simulated climate Data, and I will have to perform statistical Tests and work with a lot of Data, so maybe someone also could tell me in what concepts I should look further in (I have access to an online Python course but will not have the time to watch all lessons)

Thanks in advance

CodePudding user response:

From what I understand you want to take only the data that is there on the date 18.07.2018. The example I wrote to you below writes the date only if it is equal to 18.07.2018, but by changing the row and the columns ( you can search on an entire column or on an entire line (depends on your excel).

for i in range(len(Klimadaten)):
date = df.values[i][0]
if date == "18.07.2018":
    print(date)
    element = df.values[i][1]
    print(element)

Hope i was helpful

CodePudding user response:

Klimadaten = pd.read_csv ("Klimadaten_18-20-July.csv") Day_1 = Klimadaten[Klimadaten.Date == "18.07.2018" | (Klimadaten.Date == "18.07.2018" & Klimadaten.Time == "12:00 AM")]

basically what it means is: bring me all the rows that date is 18.07.2018 OR (date is 19.07.2018 AND time is 12:00 AM)" so you can construct more complex queries like that :)

CodePudding user response:

With help of the the Pandas Documentation I figured out the right syntax for my problem:

Day_1 = Klimadaten[(Klimadaten["Date"] == "18.07.2018") | (Klimadaten["Date"] == "19.07.2018") & (Klimadaten["Time"] == "12:00:00 AM")]
  • Related