Pandas Dataframe Functionality - dataframe

I would like to create dataframes using excel spreadsheets as source data. I need to transform the data series from the format used to store the data in the excel spreadsheets to the dataframe variable end product.
I would like to know if users have experience in using various python methods to accomplish the following:
-data series transform: I have a series that includes one data value per month, but would like to expand the table of values to include one value per day using an index (or perhaps column with date values). So if table1 has a month based index and table2 has a daily index how can I convert table1 values to the table2 based index.
-dataframe sculpting: the data I am working with is not similar in length, some data sets are longer than others. By what methods is it possible to find the shortest series length in a column in the context of a multicolumn dataframe?
Essentially, I would like to take individual tables from workbooks and combine them into a single dataframe that uses a single index value as the basis for their presentation. My workbook tables may have data point frequencies of daily, weekly, or monthly and I would like to build a dataframe that uses the daily index as a basis for the table elements while including an element for each day in series that are weekly and monthly.
I am looking at the Pandas library, but perhaps there are other libraries that I have overlooked with additional functionality.
Thanks for helping!

For your first question, try something like:
df1 = df1.resample('1d').first()
df2.merge(df1)
That will upsample your monthly or weekly dataframes and merge it with your daily dataframe. Take a look at the interpolate method to fill in missing values. To get the name of the shortest column try this out:
df.count().idxmin()
Hope that helps!

Related

Is there a way to make separate dataframes from one dataframe?

I have a dataframe containing stock prices from different dates ranging from 2017 to 2022. The dates are set as the index and they are formatted as YYYY-MM-DD. I'm trying to create separate dataframes for each year. Is there a way I can filter my data to do so?
I am new to python and am not sure of how to do this. I tried to use .iloc to pick the specific year I wanted, but it didn't work since the dates are formatted as YYY-MM-DD.

Flattening time-series IoT data with pandas

Really appreciate some help in transforming a flat Excel table into a single column series.
The Excel data reflects daily IoT sensor data (365 days/year as rows) collected hourly (24 columns of observations by hour). Current presentation from Excel file is hourly readings (columns) and dates (rows). I'm new to Stack Overflow, so I cannot directly embed images yet.
Before 1:
After: 2:
I have successfully imported the .xls file with pd.read_excel, datetime type is set for the index column, and file is imported with skiprows/skipfooter
Problem #1: How to flatten file/transpose the multi-column dataframe into a single series by hour/date.
Problem #2: How to create a multiindex that combines the date of the observation with the hour of the observation.
The following images show where the data is and where it needs to go.
I apologize in advance for any lacks in posting protocols. As I mentioned, I'm new and therefore limited in what I can post to make it easier for you to assist.
You can use df.stack() for this.
See df.stack() for more information on usage
just using df.stack() will also automatically create a multiindex with the date and hour.

running a for loop on a daterange using pyspark

I have a dataframe where one of the columns contains a date range from 2019-01-01 to 2019-02-01 where the format is:
yyyy-mm-dd Is there a way to loop over the dataframe by each day, selecting a day and then filter by that day. I would like to do some calculations on the filtered dataframe since each day has multiple records.
Since this is distributed computing, one method I came across is to insert a row number column using row_number() over a window of the entire dataframe and then run the for loop. But I feel that this is counterproductive, since I would be forcing the entire dataframe into a single node and my dataframe has millions of rows.
Is there a way to a for or while loop in the pyspark dataframe without using window function?
Your expert insights would be greatly welcomed!
Thank You

Manipulating time series with different start dates

I'm a novice Python programmer and I have an issue that I was hoping you could help me with.
I have two time series in Pandas, but they start at different dates. Let's say one starts in 1989, and the other in 2002. Now I want to to compare the cumulative growth of the two, by indexing both series to 2002 (the first time period where I have data for both), and calculate the ratio.
What is the best way to go about it? Ideally, the script should check what's the earliest available data for a pair of series and index both to 100 from that point onward.
Thank you in advance!
A practical solution may be to split the dataframe into two columns, one for each time series, and add a 'monthyear' column to each dataframe, that only lists the month and year (e.g. 05-2015). Then, you can use pd.merge on both dataframe on that month variable, keeping only the rows that have overlapping months in which they occur. The function would be pd.merge(df1, df2, on='monthyear', how='inner')
You can split the pandas dataframe by creating a new dataframe and loading in only 1 column (or row, depending on how your dataframe looks like). df1 = pd.Dataframe(original_dataframe[0]) and df2 = pd.Dataframe(original_dataframe[1])

pandas average relative elements from multi files

I want to average several .csv files which are same dimension, and save as a new file. such as:
df_new=avergae(df1,df2,df3.....)
Are there any ready function carrying out it?
Suppose all your DF columns are numeric and you want to take the mean value across multiple DFs, you can do something like this.
pd.DataFrame(columns=df1.columns, data = np.mean(np.array([df1.values,df2.values,df3.values]),axis=0))