Here is a simple DataFrame: Pivot method 1 The data can be pivoted to this: Downside: data in the 2nd row is lost because df[‘b’][1] == None. Pivot method 2 Downside: column b is lost. How can the two methods be combined so that columns b and the 2nd row are kept like so: More generally: How can information from
Tag: pandas
Python Geo-Spatial Coordinate Format Conversion
I have a dataframe containing 6 columns of coordinate pairs: (for both latitude and longitude). This is known as the NAD83 format. I want to convert these into a new dataframe of only 2 columns in decimal format, known as NAD27. The library I typically use, geopy supports virtually every format, so there actually isn’t a dedicated conversion function. I
Plot elapsed time on x axis using date indexed time-series data
In my pandas dataframe, my time series data is indexed by absolute time (a date of format YYYY-MM-DD HH24:MI:SS.nnnnn): How can I plot this data such that the x-axis of my plots is an increasing value of some interval of seconds (e.g, “0 10 20 30 40 50”) relative to the timestamp of my first sample ? Do I need
Python pandas write resulted dataframe to xlsm without losing the macro
I have a lot of excel files that I need to compile into a single excel file, and then copy the compiled one into an existing excel file (with macro / .xlsm) in a certain sheet. I solved the first problem (compiling multiple excel files into a single excel file). The resulted dataframe is saved in .csv format. The resulted
How to insert javascript code into Jupyter
I’m trying to insert this script on custom.js. I changes to color red all the negative currency. I want it to be applied to all pandas dataframes printed on Jupyter. After adding it to all custom.js available on jupyter/anaconda folders, it still didn’t change anything. Can someone help me? Answer
How to get the index of ith item in pandas.Series or pandas.DataFrame?
I’m trying to get the index of 6th item in a Series I have. This is how the head looks like: For getting the 6th index name (6th Country after being sorted), I usually use s.head(6) and get the 6th index from there. s.head(6) gives me: and looking at this, I’m getting the index as United Kingdom. So, is there
Select multiple ranges of columns in Pandas DataFrame
I have to read several files some in Excel format and some in CSV format. Some of the files have hundreds of columns. Is there a way to select several ranges of columns without specifying all the column names or positions? For example something like selecting columns 1 -10, 15, 17 and 50-100: I need to know how to do
What is as_index in groupby in pandas?
What exactly is the function of as_index in groupby in Pandas? Answer print() is your friend when you don’t understand a thing. It clears out doubts many times. Take a look: Output: When as_index=True the key(s) you use in groupby() will become an index in the new dataframe. The benefits you get when you set the column as index are:
Pandas: filter dataframe with type of data
I have dataframe. It’s a part How filter df with type? Usually I do it with str.contains, maybe it’s normal to specify any like df[df.event_duration.astype(int) == True]? Answer If all the other row values are valid as in they are not NaN, then you can convert the column to numeric using to_numeric, this will convert strings to NaN, you can
Pandas Timedelta in months
How can I calculate the elapsed months using pandas? I have write the following, but this code is not elegant. Could you tell me a better way? Answer Update for pandas 0.24.0: Since 0.24.0 has changed the api to return MonthEnd object from period subtraction, you could do some manual calculation as follows to get the whole month difference: Wrap