Unpivot data with dynamic Date columns [duplicate] - pandas

This question already has answers here:
How do I melt a pandas dataframe?
(3 answers)
Closed 8 months ago.
I need to unpivot a dataset with the column names in date format. To un-pivot, I would need to call column names, but they would keep changing every month; hence, I cannot use column names because of their dynamic nature.
Here is an example of the table:
I need to un-pivot these date columns and I cannot call column names as these columns may change next month. Here is the desired output:
Could you please help me with any solution for this in spark SQL or Pandas as I am using Palantir foundry.
Note:
There are hundreds of rows in data, this only one row is a sample example.
If I rename column names, changing them back to date columns would be difficult as well.
Thanks.

You can use melt() to achieve this
pd.melt(df, id_vars=df.columns[:3], value_vars=df.columns[3:],
var_name='Date', value_name='Value')

Related

How to remove some part from String from Pandas [duplicate]

This question already has answers here:
Extracting just Month and Year separately from Pandas Datetime column
(13 answers)
Closed last year.
I only want the Month from this Column and want to drop all the rest of the values how do I do that. Have checked all then StackOverFlow other questions nothing seems top work.
Can someone please help me out :D
This looks like a datetime object, but if not you'll want to convert it to one with:
df['created_at']= pd.to_datetime(df['created_at'])
Once it's a datetime object, you can create a month column with the following:
df['month'] = df['created_at'].dt.month

How to read in a number from a csv as a time in Pandas [duplicate]

This question already has answers here:
Convert number to time in Python
(3 answers)
Add Leading Zeros to Strings in Pandas Dataframe
(7 answers)
Closed 1 year ago.
I have a csv that I'm trying to parse with Pandas, and there's a column that contains a 24 hour time, but it's just a simple number in the csv:
CaseNumber
OccurDate
OccurTime
OffenseCategory
OffenseType
20-X5397620
1/1/20
345
Assault Offenses
Simple
Should I join it with OccurDate so I can do the normal datetime parsing with Pandas?
first of all, please check the "OccureTime" dataType using df.info(). for me it does not look 24H data column.
Actually I think this answer is the one I want:
Convert number to time in Python
All I'm looking for is the time, and the date part of the datetime is irrelevant for my needs.
Also this answer to help pad some of those values that are only two digits out to 4: Add Leading Zeros to Strings in Pandas Dataframe

How to check the column having date in proper format in postgesql? [duplicate]

This question already has answers here:
Check whether string is a date Postgresql
(2 answers)
Closed 1 year ago.
I have dataset as like below,
01-03-2021
02-04-2021
01-032021
04-05-2021
Initially this column is considered as string and i need condition check this column date is greater or lesser than another column in another table.
I can able to perform condition check between two date columns but the case was one row contains invalid date format in dataset "01-032021" i need to avoid that condition check particularly for this type of case row.
my db is postgresql, i tried to ISDATE(column) which is only useful in sql server but not find any function like this in postgresql to check the column is in date format.
Any one suggest me way to do this?
maybe you can use pg_typeof (column), but if you want to change the column to date format maybe this can help:
SELECT (substring((REPLACE(your_column,'-','')) FROM 1 for 2) || '-' || substring((REPLACE(your_column,'-','')) FROM 3 for 2) || '-' || substring((REPLACE(your_column,'-','')) FROM 5 for 4))::DATE as new_column FROM your_table

How to Filter Data with Multiple Parameters

I'd like to tally a series of data based on the day and user name. The data is being fed from a query, and I am not looking to use a pivot table because I would like to archive the data past what the program I am pulling the data from stores. Below is a sample of the data I have collected.
I want to tally the Column D "FULL_PLLT_QTY", but only for the date in Column G "SHIFT_DT" and the Column I "Name".
EX. I want to tally Column D for 6/7/2107 for Smith, R.W.
Is there a way to do this for a large range of dates and names? Also, the names appear on multiple dates. Any help with this would be much appreciated!
=SUMIFS(D:D,G:G,"6/7/2017 0:00",I:I,"Smith, R.W.")
Matching the date condition will depend on the exact format of column G. The above assumes it is just a string. If ti is a date then you probably need to use =DATE(2017,7,6) instead of the literal string "6/7/2017 0:00".

How to filter an excel columns which contain date/time as string

I have an excel sheet to filter a Column. The column relates to total experience of a person. The values are like 5years 2Months, 32Years 6Months etc... all the values are in String format. I need the following functionality.
when i enter >5 in a textbox(which i will create in a form), it should display only experience which are less than 5(filtering) . I need an idea how to do this in vba.
Can anyone help..? I just need a way to do this.
Consider the following screenshot. Column a has the unfortunate text with years and months.
Column B splits out the years. Column C splits out the months. Column D has the total number of months for the time frame. With this in place, you can filter by any of the columns using the filter options of the Autofilter built into an Excel table.
The formulas are as follows:
Years: =MID([#total],1,FIND("Years",[#total])-1)+0
Months: =MID(SUBSTITUTE([#total],"Months",""),FIND(" ",[#total])+1,99)+0
Duration in months: =([#years]*12)+[#months]
Now just use the filters in the drop down butttons of the column headers and there is no need for VBA at all.