Skip to content
Advertisement

Tag: pandas

Pandas: convert dtype ‘object’ to int

I’ve read an SQL query into Pandas and the values are coming in as dtype ‘object’, although they are strings, dates and integers. I am able to convert the date ‘object’ to a Pandas datetime dtype, but I’m getting an error when trying to convert the string and integers. Here is an example: Converting the df[‘date’] to a datetime works:

Stack columns above value labels in pandas pivot table

Given a dataframe that looks like: Key1 Key2 Value1 Value2 0 one A 1.405817 1.307511 1 one B -0.037627 -0.215800 2 two C -0.116591 -1.195066 3 three A 2.044775 -1.207433 4 one B -1.109636 0.031521 5 one C -1.529597 1.761366 6 two A -1.349865 0.321454 7 three B 0.814374 2.285579 8 one C 0.178702 0.479210 9 one A 0.718921 0.504311

Python: Convert map in kilometres to degrees

I have a pandas Dataframe with a few million rows, each with an X and Y attribute with their location in kilometres according to the WGS 1984 World Mercator projection (created using ArcGIS). What is the easiest way to project these points back to degrees, without leaving the Python/pandas environment? Answer Many years later, this is how I would do

How to filter a pandas series with a datetime index on the quarter and year

I have a Series, called ‘scores’, with a datetime index. I wish to subset it by quarter and year pseudocode: series.loc[‘q2 of 2013’] Attempts so far: s.dt.quarter AttributeError: Can only use .dt accessor with datetimelike values s.index.dt.quarter AttributeError: ‘DatetimeIndex’ object has no attribute ‘dt’ This works (inspired by this answer), but I can’t believe it is the right way to

Could pandas use column as index?

I have a spreadsheet like this: I don’t want to manually swap the column with the row. Could it be possible to use pandas reading data to a list as this: Answer Yes, with pandas.DataFrame.set_index you can make ‘Locality’ your row index. If inplace=True is not provided, set_index returns the modified dataframe as a result. Example:

Advertisement