Data-set manipulation to find temp difference in Python

134
November 20, 2019, at 9:00 PM

I have the following data-set:

oxford, 20180101, tempmax, 140
oxford, 20180101, tempmin, 40
oxford, 20180102, tempmax, 160
oxford, 20180102, tempmin, 20
oxford, 20180103, tempmax, 90
oxford, 20180103, tempmin, 10
oxford, 20180104, tempmax, 200
oxford, 20180104, tempmin, 120

How can I read this file in, combine by location and date, and work out the temp difference in python?

example output:

oxford, 20180101, tdiff, 40
oxford, 20180102, tdiff, 160
oxford, 20180103, tdiff, 80
oxford, 20180104, tdiff, 80

What sort of manipulation in python could be used for this?

Answer 1

Try this

import pandas as pd
df = pd.DataFrame({"A":["oxford","oxford","oxford","oxford","oxford","oxford","oxford","oxford"],
                   "B":["20180101","20180101","20180102","20180102","20180103","20180103","20180104","20180104"],
                    "C":["tempmax","tempmin","tempmax","tempmin","tempmax","tempmin","tempmax","tempmin"],
                    "D":[140,40,160,20,90,10,200,120]})

df["B"] = pd.to_datetime(df["B"])
dfnew = df.sort_values(["B"]).groupby(['A', 'B'])["D"].apply(lambda x: (max(list(x))-min(list(x)))).reset_index()
dfnew["diff"] = "tdiff"

//output
        A          B    D   diff
0  oxford 2018-01-01  100  tdiff
1  oxford 2018-01-02  140  tdiff
2  oxford 2018-01-03   80  tdiff
3  oxford 2018-01-04   80  tdiff
Rent Charter Buses Company
READ ALSO
How do I add a word to the middle of a table in python?

How do I add a word to the middle of a table in python?

I wrote code that prints an empty 20x20 tableThe main goal is to eventually make a program that creates a crossword

116
kivy custom widget bind error

kivy custom widget bind error

I am trying to make a small tetris game for learning python with kivyI am trying to create a custom widget with size 20,20

152
How do I calculate deferred annuity using python higher order function method?

How do I calculate deferred annuity using python higher order function method?

An annuity is a series of payments made at fixed intervals of timeIf you buy an annuity from a bank, you will pay a principal sum upfront, and then receive monthly or yearly payouts deducted from the principal

135
How do I print a binary number's digits individually Python?

How do I print a binary number's digits individually Python?

I am trying to understand how to print off a binary numbers digits individuallyFor example, if the binary number is 1011, I want to print to the console:

116