Pythonic way to create pandas dataframe based on if-else condition for nd-array and 1d-array

Opps_0

I have 3 methods to do some simple calculations. 2 methods are returning nd-array and 1 method returns 1d-array. Later, I am creating a pandas dataframe based on the return output from the methods.

While I am creating pandas dataframe, I am also calculating std from the method's result. For nd-array I need to use axis=0 and axis=1 to calculate std but for the 1d-array, I can not use the axis properties.

That means I need to use if-else to calculate std for different returns from the methods. Below code is working fine

def main_fn(arr_1):
    all_result_summary = []
    for method in ["met_1", "met2", "met_3"]:
        results: ndarray = np.array(main_fn(list(arr_1), method))
        if method == "met_3":
            all_result_summary.append(
                    pd.DataFrame(
                        {
                            "Method": method,
                            "result": results.mean(),
                            "result_sd_ax_0": results.std(ddof=1),
                            "result_sd_ax_1": "NA",
                        },
                        index=[0],
                    )
            )
        else:
            all_result_summary.append(
                pd.DataFrame(
                    {
                        "Method": method,
                        "result": results.mean(),
                        "result_sd_ax_0": results.mean(axis=0).std(ddof=1),
                        "result_sd_ax_1": results.mean(axis=1).std(ddof=1),
                    },
                    index=[0],
                )
            )
        summary = pd.concat(all_result_summary, axis=0, ignore_index=True)
    return summary

However, I wanted to use a more pythonic way instead of reusing the whole code using if-else. Any other way to optimize the code?

richardec

How about use x if cond else y, ternary-style syntax?

def main_fn(arr_1):
    all_result_summary = []
    for method in ["met_1", "met2", "met_3"]:
        results: ndarray = np.array(main_fn(list(arr_1), method))
        all_result_summary.append(
            pd.DataFrame(
                {
                    "Method": method,
                    "result": results.mean(),
                    "result_sd_ax_0": results.std(ddof=1) if method == "met_3" else results.mean(axis=0).std(ddof=1),
                    "result_sd_ax_1": "NA" if method == "met_3" else results.mean(axis=1).std(ddof=1),
                },
                index=[0],
            )
        )
        summary = pd.concat(all_result_summary, axis=0, ignore_index=True)
    return summary

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Arrays based on nd array condition

From Dev

Pythonic way to create a dictionary from array based index values

From Dev

create new pandas dataframe column based on if-else condition with a lookup

From Dev

Pythonic/Numpy way of converting a 1D array into 2D vector array of indexed values

From Dev

is there a pythonic way to change scalar and 0d-array to 1d array?

From Dev

Pythonic way to assign 3rd Dimension of Numpy array to 1D Array

From Dev

Pythonic way to select elements of an array based on values?

From Dev

Pandas dataframe if else condition based on previous rows

From Dev

Is there a vectorized way of appending/indexing values from nd_array 2 based off component r/c values in nd_array 1?

From Dev

Slicing based on a condition, else return empty array

From Dev

np.concatenate a ND tensor/array with a 1D array

From Dev

Efficient/Pythonic way to Filter pandas DataFrame based on priority

From Dev

Efficient/Pythonic way to create lists from pandas Dataframe column

From Dev

What is the more efficient way to create a pairwise 2D array for a 1D numpy array?

From Dev

Create column in pandas dataframe based on condition

From Dev

Python Pandas: Create dataframe based on condition

From Dev

Create a Pandas Dataframe column based on multiple condition

From Dev

how to declare an array based on if else condition inside a function and calling this array

From Dev

Reshaping an array from 1D to ND in C

From Dev

How to compute distance formula between a 1d and an nd array?

From Dev

How to convert ND array to 1D and revert it back

From Dev

Pythonic way to create array with entries unique for row and column

From Java

Pythonic way to create a numpy array from a list of numpy arrays

From Dev

Fastest way summing condition based part of array

From Dev

Fast way to round array values based on condition

From Dev

Is there a way to initialize a public array based on a condition?

From Dev

is there a way to count columns in an array based on a condition?

From Dev

Assign values in pandas based on condition on array of values

From Dev

Merge 3D numpy array into pandas Dataframe + 1D vector

Related Related

  1. 1

    Arrays based on nd array condition

  2. 2

    Pythonic way to create a dictionary from array based index values

  3. 3

    create new pandas dataframe column based on if-else condition with a lookup

  4. 4

    Pythonic/Numpy way of converting a 1D array into 2D vector array of indexed values

  5. 5

    is there a pythonic way to change scalar and 0d-array to 1d array?

  6. 6

    Pythonic way to assign 3rd Dimension of Numpy array to 1D Array

  7. 7

    Pythonic way to select elements of an array based on values?

  8. 8

    Pandas dataframe if else condition based on previous rows

  9. 9

    Is there a vectorized way of appending/indexing values from nd_array 2 based off component r/c values in nd_array 1?

  10. 10

    Slicing based on a condition, else return empty array

  11. 11

    np.concatenate a ND tensor/array with a 1D array

  12. 12

    Efficient/Pythonic way to Filter pandas DataFrame based on priority

  13. 13

    Efficient/Pythonic way to create lists from pandas Dataframe column

  14. 14

    What is the more efficient way to create a pairwise 2D array for a 1D numpy array?

  15. 15

    Create column in pandas dataframe based on condition

  16. 16

    Python Pandas: Create dataframe based on condition

  17. 17

    Create a Pandas Dataframe column based on multiple condition

  18. 18

    how to declare an array based on if else condition inside a function and calling this array

  19. 19

    Reshaping an array from 1D to ND in C

  20. 20

    How to compute distance formula between a 1d and an nd array?

  21. 21

    How to convert ND array to 1D and revert it back

  22. 22

    Pythonic way to create array with entries unique for row and column

  23. 23

    Pythonic way to create a numpy array from a list of numpy arrays

  24. 24

    Fastest way summing condition based part of array

  25. 25

    Fast way to round array values based on condition

  26. 26

    Is there a way to initialize a public array based on a condition?

  27. 27

    is there a way to count columns in an array based on a condition?

  28. 28

    Assign values in pandas based on condition on array of values

  29. 29

    Merge 3D numpy array into pandas Dataframe + 1D vector

HotTag

Archive