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])
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]
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)]
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']
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.