I have a Dataframe df_main
with the following columns:
ID | Category | Time | Status | XYZ |
---|---|---|---|---|
1 | A | value | value | value |
2 | B | value | value | value |
3 | C | value | value | value |
4 | D | value | value | value |
5 | E | value | value | value |
Using the following code, I have created new Dataframes based on Categories in the table. I have created a Dataframe dictionary and created the dataframes in this format df_A
, df_B
, df_C
...
I have stored the row in the new Dataframes equivalent to the Category Name. So, df_A
will have the row from df_main
which has the Category value "A".
Code:
dict_of_df = {} # initialize empty dictionary
i=0
for index, row in df_main.iterrows():
if i<5:
newname = df_main['Category'].values[i]
dict_of_df["df_{}".format(newname)] = row
i=i+1
I want to print the dataframes by their dataframe name, and not by iterating the dictionary. It should be like this:
print(df_A)
print(df_B)
print(df_C)
print(df_D)
print(df_E)
How can I achieve this? A solution without using a dictionary would work too. Any solution is fine as long as I am able to store a row of a specific Category in a new Dataframe specific to Category Name and print it using the Dataframe name.
Let me know if more details are required.
Edit:
This link is somewhat similar to my use case: Using String Variable as variable name
I wanted to be specific to dataframes, as my end goal was to print the dataframes by their names.
The method mentioned in the answers of that link is specific to variables and would need a different code solution using the exec
method for dataframes.
The idea behind this code is to include it in Power BI. Get Source using python script in Power BI accepts dataframes as tables, for which, I would have to declare or print a dataframe in the code.