dataframe

Pandas apply casts None dtype to object or float depending on other outputs

Pandas apply casts None dtype to object or float depending on other outputs Question: I would like to control the output dtypes for apply on a row. foo and bar below have multiple outputs. import pandas as pd def foo(x): return x[‘a’] * x[‘b’], None, x[‘a’] > x[‘b’] def bar(x): return x[‘a’] * x[‘b’], None …

Total answers: 1

Splitting a column with delimiter and place a value in the right column

Splitting a column with delimiter and place a value in the right column Question: I have a data frame with a column that potentially can be filled with 3 options (a,b, and/or c) with a comma delimiter. import pandas as pd df = pd.DataFrame({‘col1’:[‘a,b,c’, ‘b’, ‘a,c’, ‘b,c’, ‘a,b’]}) I want to split this column based …

Total answers: 3

Solving incompatible dtype warning for pandas DataFrame when setting new column iteratively

Solving incompatible dtype warning for pandas DataFrame when setting new column iteratively Question: Setting the value of a new dataframe column: df.loc[df["Measure] == metric.label, "source_data_url"] = metric.source_data_url now (as of Pandas version 2.1.0) gives a warning, FutureWarning: Setting an item of incompatible dtype is deprecated and will raise in a future error of pandas. Value …

Total answers: 3

setting a multi index does not allow access using .loc?

setting a multi index does not allow access using .loc? Question: import pandas as pd df = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]}) empty_row_index = ("zero_scan") empty_row_df = pd.DataFrame({c: [None] for c in df.columns}) empty_row_df.index = [empty_row_index] df2 = pd.concat([empty_row_df, df])result = df2.loc[df2.index[0], :] works import pandas as pd df = pd.DataFrame({"a": [1, …

Total answers: 2

Filling DF's NaN/Missing data from another DF

Filling DF's NaN/Missing data from another DF Question: I have two data frames: df1 = pd.DataFrame({‘Group’: [‘xx’, ‘yy’, ‘zz’, ‘x’, ‘x’, ‘x’,’z’,’y’,’y’,’y’,’y’], ‘Name’: [‘A’, ‘B’, ‘C’, None, None, None, None, None, None, None, None], ‘Value’: [5, 3, 4, 7, 1, 3, 6, 5, 9, 5, 4]}) df2 = pd.DataFrame({‘Name’: [‘A’, ‘A’, ‘B’, ‘B’], ‘Group’: [‘x’, …

Total answers: 1

2D to 3D numpy array by blocks

2D to 3D numpy array by blocks Question: I have the following 2D dataframe conc corresponding to gas concentrations on 4 layers at a series of wavelengths wl : conc = wl gas1 gas2 gas3 layer 0 5000 10 13 250 1 1 5000 20 14 260 2 2 5000 30 15 270 3 3 …

Total answers: 4

Changing values in each row of a column based on values in other columns of the corresponding row (Python/Pandas)

Changing values in each row of a column based on values in other columns of the corresponding row (Python/Pandas) Question: data = [{‘a’: 12, ‘b’: 2, ‘c’: 3, ‘d’: ‘bat’}, {‘a’: ‘NaN’, ‘b’: 20, ‘c’: 30, ‘d’: ‘ball’}, {‘a’: 4, ‘b’: 20, ‘c’: 30, ‘d’: ‘pin’}] df = pd.DataFrame(data) I’m having a hard time figuring …

Total answers: 2

How to use square brackets as part of a variable name in pandas?

How to use square brackets as part of a variable name in pandas? Question: I am trying to use pandas.assign function. The function adds another column into an existing column. For example DataFrame1 = DataFrame1.assign(NewColName=[1,2,3,4]) Adds a new column named "NewColName". However, my ideal column name would be "Weight [kg]", and when I try to …

Total answers: 1