David
David

Reputation: 339

Is there an easier way to grab a single value from within a Pandas DataFrame with multiindexed columns?

I have a Pandas DataFrame of ML experiment results (from MLFlow). I am trying to access the run_id of a single element in the 0th row and under the "tags" -> "run_id" multi-index in the columns.

enter image description here

The DataFrame is called experiment_results_df. I can access the element with the following command:

experiment_results_df.loc[0,(slice(None),'run_id')].values[0]

I thought I should be able to grab the value itself with a statement like the following:

experiment_results_df.at[0,('tags','run_id')]
# or...
experiment_results_df.loc[0,('tags','run_id')]

But either of those just results in the following rather confusing error (as I'm not setting anything):

ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (2,) + inhomogeneous part.

It's working now, but I'd prefer to use a simpler syntax. And more than that, I want to understand why the other approach isn't working, and if I can modify it. I find multiindexes very frustrating to work with in Pandas compared to regular indexes, but the additional formatting is nice when I print the DF to the console, or display it in a CSV viewer as I currently have 41 columns (and growing).

Upvotes: 0

Views: 144

Answers (1)

Corralien
Corralien

Reputation: 120409

I don't understand what is the problem:

df = pd.DataFrame({('T', 'A'): {0: 1, 1: 4},
                   ('T', 'B'): {0: 2, 1: 5},
                   ('T', 'C'): {0: 3, 1: 6}})
print(df)

# Output
   T      
   A  B  C
0  1  2  3
1  4  5  6

How to extract 1:

>>> df.loc[0, ('T', 'A')]
1

>>> df.at[0, ('T', 'A')]
1

>>> df.loc[0, (slice(None), 'A')][0]
1

Upvotes: 1

Related Questions