multi-index

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

Create a multi-index data-frame

Create a multi-index data-frame Question: I have the following code to create a multi-indexed data frame: import pandas as pd import numpy as np # Define the data data = { (‘rf’, ‘wv_pretrained’): (0.7392722279437006, 0.7412604086615894), (‘rf’, ‘wv_custom’): (0.7746309646412634, 0.7762235207436783), (‘rf’, ‘glove_pretrained’): (0.7411603158256094, 0.7427841615992615), (‘rf’, ‘spacy_pretrained’): (0.731719876416066, 0.7338888018745795), (‘rf’, ‘sent_trf’): (0.7229660144181257, 0.7242986991569383), (‘rf’, ‘bert_trf’): (0.7126673532440783, 0.7139687043942123), …

Total answers: 1

How do I select a subset of a DataFrame based on a condition on a column

How do I select a subset of a DataFrame based on a condition on a column Question: Similar to How do I select a subset of a DataFrame based on one level of a MultiIndex, let df = pd.DataFrame({"v":[x*x for x in range(12)]}, index=pd.MultiIndex.from_product([["a","b","c"],[1,2,3,4]])) and suppose I want to select only rows with the v …

Total answers: 1

How do I select a subset of a DataFrame based on one level of a MultiIndex

How do I select a subset of a DataFrame based on one level of a MultiIndex Question: Let df = pd.DataFrame({"v":range(12)}, index=pd.MultiIndex.from_product([["a","b","c"],[1,2,3,4]])) and suppose I want to select only rows with the first level being a or c: v a 1 0 2 1 3 2 4 3 c 1 8 2 9 3 10 …

Total answers: 1

Assign to a whole column of a Pandas DataFrame with MultiIndex?

Assign to a whole column of a Pandas DataFrame with MultiIndex? Question: I have a DataFrame(called midx_df) with a MultiIndex, I want to assign values from a whole column of another DataFrame(called sour_df) with single level index to midx_df. All of index values of sour_df exist in the top level index of midx_df, I need …

Total answers: 1

reading multi-index header based excel file using pandas

reading multi-index header based excel file using pandas Question: I have an excel file where first 3 rows have header names, I want to read it in pandas but facing difficulty in the multi-index header. PLAN 2023 Traffic per channel Traffic Share per Channel month week All Traffic red green orange red green orange jan …

Total answers: 1

Setting values in a pandas multi-index cross-sectional slice

Setting values in a pandas multi-index cross-sectional slice Question: I would like to set the value of a cross section to the value relative to the mean. The code below sets the values to null, but I would like the values to be -5 and 5. Is there an easily readable way to do this …

Total answers: 1

Getting index counter on a data frame

Getting index counter on a data frame Question: I have the following data frame: import pandas as pd data = { "id_1": [1, 1, 1, 2, 2, 2], "id_2": [1, 1, 1, 2, 2, 2], "foo": [0.1, 0.1, 0.1, 0.2, 0.2, 0.2], } df = pd.DataFrame(data) df = df.set_index(["id_1", "id_2"]) which looks like this: foo …

Total answers: 2

Merge Select Columns Dataframe Columns Into a Multi-Index

Merge Select Columns Dataframe Columns Into a Multi-Index Question: I have N dataframes, in this case lets use 2 dfs as an example: df1 = pd.DataFrame([[‘a’, 2], [‘b’, 4]], columns=[‘foo’, ‘bar’]) df2 = pd.DataFrame([[‘a’, 3], [‘b’, 5]], columns=[‘foo’, ‘bar’]) Which produce: foo bar 0 a 2 1 b 4 foo bar 0 a 3 1 …

Total answers: 1