Pandas new column based on value from another column

How can I add a new column to dataframe that has the same name as values in another column? My dataframe is: import pandas as pd df = pd.DataFrame({ ‘Col1’: [2, 74, 37, 36, 95], ‘Col2’: [36, 80, 9, 26, 16], ‘Col3’: [20, 4, 7, 55, 91], ‘Col4’: [‘Col2′,’Col2′,’Col2′,’Col2′,’Col2’] }) So I need to add… Read More Pandas new column based on value from another column

How do you mix 1d and 2d variables in a pandas dataframe?

I am working with a bucketload of data that has the form: import pandas as pd import numpy as np lat = np.array([80.589, 80.592, 80.595]) lon = np.array([50.268, 50.264, 50.260]) wav = np.array([[486, 605, 666, 821, 777, 719], [ 65, 60, 68, 67, 72, 64], [866, 946, 882, 855, 999, 1195]]) print("lat shape:",lat.shape) print("lon shape:",lon.shape)… Read More How do you mix 1d and 2d variables in a pandas dataframe?

How to remove rows from MultiIndex Dataframe

I have pandas multi-index dataframe that like this. I need to remove rows where bnds equals to 1.0. I’ve tried to do df_f.drop(‘1.0′, level=bnds, axis=0, inplace=True) as per documentation, but getting error NameError: name ‘bnds’ is not defined Honolulu time_bnds Seattle bnds time 1.0 2015-01-01 12:00:00 70.277412 2015-01-01 00:00:00 13.346752 2015-01-02 12:00:00 69.948593 2015-01-02 00:00:00… Read More How to remove rows from MultiIndex Dataframe

Is a pandas.DataFrame still sorted after using the method `query`?

I am working on a dataframe df in python. I need to query and sort the results multiple times, but on different columns: for x in X: # query the dataframe and sort the result query_result = df.query(f"column_name == ‘{x}’").sort_values(by="other_column") # … use query_result … I am wondering if I can factorize the sorting operation,… Read More Is a pandas.DataFrame still sorted after using the method `query`?

Pandas merge complaining about non unique labels when key is a composite and unique

I am trying to merge two dataframes such that i end up with one with same number of columns but but an increased row count. import pandas as pd, numpy as np data1 = [[‘date’ , ‘symbol’, ‘value’], [‘1999-01-10’, ‘AAA’, 101], [‘1999-01-11’, ‘AAA’, 201]] I am trying to merge two dataframes such that i end… Read More Pandas merge complaining about non unique labels when key is a composite and unique

Unexpected output from pandas' DataFrameGroupBy.diff function

Consider the following piece of python code, which is essentially copied from the first code insert in the Transformation section of pandas‘ user guide’s Group by: split-apply-combine chapter. import pandas as pd import numpy as np speeds = pd.DataFrame( data = {‘class’: [‘bird’, ‘bird’, ‘mammal’, ‘mammal’, ‘mammal’], ‘order’: [‘Falconiformes’, ‘Psittaciformes’, ‘Carnivora’, ‘Primates’, ‘Carnivora’], ‘max_speed’: [389.0,… Read More Unexpected output from pandas' DataFrameGroupBy.diff function