How can I sort date array [duplicate] - python

This question already has answers here:
Sort a Python date string list
(2 answers)
Sort list of date strings
(2 answers)
Closed 5 years ago.
Please see my following code snippet
Input
list_x = ["11/1/2100", "5/12/1999", "19/1/2003", "11/9/2001"]
Output
['5/12/1999', '11/9/2001', '19/1/2003', '11/1/2100']

You can convert your day/month format to day in year format and then compare each element in the list based on the year then the day

Related

How can I type to see only one row in df.head() [duplicate]

This question already has answers here:
How to print a specific row of a pandas DataFrame?
(6 answers)
Closed 11 months ago.
For example, if I want to see only one sentence in the dataframe in row 21, how can I type in the head function?
df.head(20)? df.head(19:20)
you can access elements with their integer position df.iat[row,col]
or an integer row with df.iloc([row])

How to reverse a series in pandas by Python? [duplicate]

This question already has an answer here:
Reversing the order of values in a single column of a Dataframe
(1 answer)
Closed 1 year ago.
I need to reverse a series for correct plotting.
So I wrote the code below:
dataFrame["close"] = dataFrame["close"][::-1]
But it doesn't differ. Why?
You are including the specific column. You should reverse the entire dataframe like this:
dataFrame.reindex(index=dataFrame.index[::-1])
or
dataFrame.iloc[::-1]

How can i create a list of elements from 2 other lists [duplicate]

This question already has answers here:
Remove all the elements that occur in one list from another
(13 answers)
Closed 3 years ago.
Imagine i have the following 2 lists.
List years- contains all years between 1901 and 2099
years=[1901,...,2099]
List leap years-contais all leap years between 1901 and 2099
I want to creat a non leap_years list that contains all the elements from the first list that are not leap years.
For leap years list i iterate using range(1901,2100,4) but i cant make the non leap years list. What should be the algorithm?
I actually dont have any code since this is a teoric question.
I think this is what you are looking for:
[i for i in years if i not in leap_years]
Fill a list with the numbers from years if the number is not contained in leap_years
years =[i for i in range(1901,2100)]
leap_year = [i for i in range(1904,2100,4)]
result = list(set(years)-set(leap_year))
use set property on your list is better than using if year in list
First of all, range(1901,2100,4) won't give you a list of leap years between 1901 and 2099. range(1904,2100,4) will give you that.
The algorithm could be something along the lines of (in psuedocode):
non-leap years is the empty list
for year in range(1901, 2099):
if year%4 != 0:
add year to non-leap years
A simple list-comprehension should suffice, just find all years which are not in leap_years (list of leap years) but are in all_years (list of all years)
non_leap_years = [year for year in all_years if year not in leap_years]
Another approach is to convert both to sets, and take the set difference
non_leap_years = list(set(all_years) - set(leap_years))
As an extra tidbit, we already have a calendar.isleap function which will tell you if the year is leap year
import calendar
all_years = list(range(1901,2100))
leap_years = [year for year in all_years if calendar.isleap(year)]

Pandas: converting strings with "$" to float [duplicate]

This question already has answers here:
pandas - how to convert all columns from object to float type
(3 answers)
Closed 4 years ago.
I am a beginner trying to analyze a dataset of Congressional campaign funding sources but they are all string values with '$' in them. How can I quickly change every value into a numerical value?enter image description here
states_table[dollar_columns] = states_table[dollar_columns].replace('[\$,]', '', regex=True).astype(float)
Where dollar_columns is a list of the columns you want to convert. For instance:
dollar_columns = ['net_con', 'net_ope_exp']

Subtract one date from another and get int [duplicate]

This question already has answers here:
How to calculate number of days between two given dates
(15 answers)
Closed 7 years ago.
How can I subtract on datefield from another and get result in integer?
Like 11.05.2015-10.05.2015 will return 1
I tried
entry.start_devation= each.start_on_fact - timedelta(days=data.calendar_start)
Take the difference between two dates, which is a timedelta, and ask for the days attribute of that timedelta.

Categories

Resources