Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I have a csv file of stock prices for each trading day for 9 years. how do i get the last trading day of each month and the respective prices?
I have tried grouping by months followed by the largest day but it doesn't seem to be working. Any guidance or suggestions pls
file:///var/folders/76/qqn_44f945564bdvv8dw_0jc0000gn/T/com.apple.Safari/WebKitDropDestination-wOBqM5Fs/Screenshot%202019-08-19%20at%205.03.22%20PM.png
import pandas as pd
data=pd.read_csv('csv_file')
data
type(data.index)
data.set_index('date',inplace=True)
Apologies, its my first time using this so i don't really know how to post the code. But this is the code i have so far. The url is the result of the csv data.
You can use
df.groupby([pd.Grouper(key = 'column_containing date', freq = 'M')])['column_containing date'].last()
If your date data is part of the index you can use
df.groupby(df.index.strftime('%Y-%m')).tail(1)
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 months ago.
This post was edited and submitted for review 7 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I'm new to programming. My project is a credit card award points optimizer written in Python 3. I have a few dictionaries based on spending categories, with the keys/values representing my credit cards and their point multiplier for that category.
After showing the user the categories, it asks them to input which one they want to make a purchase in, and here's where I'm getting an error because the input is a string and my dictionary is a variable.
How can I convert the string into a variable, so that I can print the keys/values?
Example category and list of categories
dining = {'amex': '4x points'}
categories = ['dining', 'grocery']
input and call script
purchase_type = input('Which category is it?: ')
for category in categories:
if purchase_type == category:
You can put your various category dictionaries into a dictionary themselves. Then you can just plug the input like selected = categories[theirInputVariable]
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I have this sample of a data frame showing population over the years.
I want to remove the row labels 'Country Code' altogether and have the next column, 'Country Name', as the row labels instead. How can I do this?
Let me know if you need more information or anything is not clear.
df = df.set_index('Country Name')
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I want to select the rows where the revenue is NaN. So, row 7 and row 43 should be selected. I tried the code (In[117]) shown in the screenshot but it doesn't work for me.
You can use isna
NaN_table[NaN_table.Revenue.isna()]
Just use isnull:
NaN_table[NaN_table['Revenue'].isnull()]
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
i want to cut all the columns of Data Frame. When I print the result it show good result, but when I want to assign those values in new data frame it returns NaNs.example of code
You need to paste your code here for understanding proper solution of problem.
you can try to add one line of code:
slc=slc.reset_index(drop=True)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm writting (just for fun) a function that prints calendar for any given month and year.
How to determine the first day of a month? Without using the calendar module.
Do you mean week day (monday, ... sunday)?
>>> from datetime import datetime
>>> datetime(2017, 11, 2).weekday() # Or another day
3
Results from 0 to 6, where 0 is monday.
http://docs.python.org/3/library/datetime.html
There's also isoweekday, isocalendar and other functions/methods in the link above that might be helpful.
Assuming you want to know how the computation is done, and not just what library module to call, the usual method is Zeller's Congruence.
http://en.wikipedia.org/wiki/Zeller%27s_congruence
That's a neat formula, based on the observation that the days in the months are pretty much regularly-spaced if you view the year as starting in March instead of January. If you look at the Latin behind the names of the months from September to December, you should see that that indeed is when the year started when the (then Julian, named for Julius Caesar) calendar was devised.
You can also search for that term to find a large number of implementation, of varying quality, in just about every programming language.
H.D's answer is good, if you need the name of the day the following should work.
>>> from datetime import datetime
>>> datetime.now().strftime("%A")
'Wednesday'
>>>