Date Time Series wise grouping of data and distribution

2024/10/10 14:30:21

I am trying the merge the datetime series with a repository data while grouping by name and summing the values.

File1.csv Timeseries,Name,count
07/03/2015 06:00:00,Paris,100
07/03/2015 06:00:00,Paris,600
07/03/2015 06:00:00,Paris,700
07/03/2015 06:00:00,London,200
07/03/2015 06:00:00,London,100
07/03/2015 06:00:00,London,500
07/03/2015 06:00:00,Dublin,300
07/03/2015 06:00:00,Dublin,400
07/03/2015 06:00:00,Dublin,400

Output

Master_file.csv (append mode)Name,Timeseries(n-1)Timeseries(n)#put the datetime series as header and put       Paris,300,1400      #Sum of all the values with same NameLondon,200,800Dublin,400,1100Program import pandas as pd 
import numpy as npdf = pd.read_csv('/home/lat_lon1.csv')
df1 = pd.read_csv('/home/lat_lon_master.csv')gp = df.groupby('Name')['date timeseries'].sum().reset_index() 
df1.merge(gp, on='Name')

I am having trouble in changing the date time column to header and putting the correct values under. Those Names not found can be given NAN and replaced in next iterations.

Answer

Please check the python pandas Data Frame documentation Click here Here is the code you are looking at.

Output

Timeseries Name count07/03/2015 06:00:00 Dublin 110007/03/2015 06:00:00 London 80007/03/2015 06:00:00 Paris 1400

   #!/bin/pythonimport pandas as pdimport numpy as npdf=pd.read_csv('/home/saiharsh/Documents/Crowd Street/Transition_Data/Telecom_7.csv') #Please enter the file Locationgp=df.groupby('Name').sum().reset_index()flag=0for i in gp['Name']:if flag==1:time=df['Timeseries'][df['Name']==i]time=time.tail(1)frames=[time1,time]time1=pd.concat(frames)else:time1=df['Timeseries'][df['Name']==i]time1=time1.tail(1)flag=1time1=time1.reset_index(drop=True)result=pd.concat([time1,gp],axis=1,join='inner')result=result.to_csv(index=False)print result

Please feel free to reply if any problem.

https://en.xdnf.cn/q/118448.html

Related Q&A

Trying to run Python in html

I am trying to run a python program in html but I am getting an error. First it says Then if I type anything it appears with this error This was the Html code <html><head><title>Antho…

Removing from a string all the characthers included between two specific characters in Python

Whats a fast way in Python to take all the characters included between two specific characters out of a string?

Pyside6: Create QTabWidget with function rather than class

Ive been trying to make an application using Pyside6 and cant seem to understand why we cant create a QDialog having QTabWidget with just functions. I am not sure if I am making a mistake somewhere so,…

Pythons End Of Life

What exactly will happen to Python 2.7 after 1/2020?I understand that Python 2.7 will no longer be supported but what will actually happen? Does it mean that decision makers will delete the whole cod…

Gathering numerical data from a string input

I would like to get user input for their credit rating e.g AAA, A, BBB etc and then assign an interest rate to this. For example, if the user has a good credit rating e.g AAA I would charge an interest…

Getting Turtle in Python to recognize click events [duplicate]

This question already has an answer here:Turtle in python- Trying to get the turtle to move to the mouse click position and print its coordinates(1 answer)Closed 5 months ago.Im trying to make Connect …

Delete last widget from gridlayout

I have a Grid layout in which I add Qlineedits at runtime. while pushing the button I want to delete the last qline edit from the gridlaout Why does this function delete all qlinedits at the same time …

Counting unique words

Question:Devise an algorithm and write the Python code to count the number of unique words in a given passage. The paragraph may contain words with special characters such as !, ?, ., , , : and ; and …

Django cant find template dir?

Originally I had just one app in my Django project, the templates consisted of an index.html a detail.html and a layout.html ... the index and detail files extended the layout. They all lived in the sa…

Python Programming Loop

Im doing an assignment where I have to conduct a quiz for different topics. This is my code so far.print("Hello and welcome to Shahaads quiz!") #Introduction name = input("What is your n…