pandas 如何在python中分配从函数调用的动态变量

ff29svar  于 2022-12-09  发布在  Python
关注(0)|答案(1)|浏览(144)

I have a function which does a bunch of stuff and returns pandas dataframes. The dataframe is extracted from a dynamic list and hence I'm using the below method to return these dataframes. As soon as I call the function (code in 2nd block), my jupyter notebook just runs the cell infinitely like some infinity loop. Any idea how I can do this more efficiently.

funct(x):
    some code which creates multiple dataframes
    i = 0
    for k in range(len(dynamic_list)):
        i += 1
        return globals()["df" + str(i)]

The next thing I do is call the function and try to assign it dynamically,

i = 0
for k in range(len(dynamic_list)):
    i += 1
    globals()["new_df" + str(i)] = funct(x)

I have tried returning selective dataframes from first function and it works just fine, like,

funct(x):
    some code returning df1, df2, df3....., df_n
    return df1, df2

new_df1, new_df2 = funct(x)
5hcedyr0

5hcedyr01#

for each dataframe object your code is creating you can simply add it to a dictionary and set the key from your dynamic list.
Here is a simple example:

import pandas as pd

test_data = {"key1":[1, 2, 3], "key2":[1, 2, 3], "key3":[1, 2, 3]}
df = pd.DataFrame.from_dict(test_data)

dataframe example:

key1  key2  key3
0     1     1     1
1     2     2     2
2     3     3     3

I have used a fixed list of values to focus on but this can be dynamic based on however you are creating them.

values_of_interest_list = [1, 3]

Now we can do whatever we want to do with the dataframe, in this instance, I want to filter only data where we have a value from our list.

data_dict = {}

for value_of_interest in values_of_interest_list:

    x_df = df[df["key1"] == value_of_interest]

    data_dict[value_of_interest] = x_df

To see what we have, we can print out the created dictionary that contains the key we have assigned and the associated dataframe object.

for key, value in data_dict.items():
    print(type(key))
    print(type(value))

Which returns

<class 'int'>
<class 'pandas.core.frame.DataFrame'>
<class 'int'>
<class 'pandas.core.frame.DataFrame'>

Full sample code is below:

import pandas as pd

test_data = {"key1":[1, 2, 3], "key2":[1, 2, 3], "key3":[1, 2, 3]}
df = pd.DataFrame.from_dict(test_data)

values_of_interest_list = [1, 3]

# Dictionary for data
data_dict = {}

# Loop though the values of interest
for value_of_interest in values_of_interest_list:

    x_df = df[df["key1"] == value_of_interest]

    data_dict[value_of_interest] = x_df

for key, value in data_dict.items():
    print(type(key))
    print(type(value))

相关问题