How Do I Create Multiple Dataframes That Are Named Based On The Value Of Another Dataframe?
I have a dataframe with about 2.5M rows and would like to split this up into about 10000 rows (one for each unique value of TICKER), and to have the name be df(i) for each value of
Solution 1:
Yes, you can do with groupby
:
for k,d in df.groupby('ticker'):
print(d)
print('-'*30)
Output:
ticker date assets
0 A 04-30-2020 40
1 A 07-30-2020 50
6 A 10-31-2020 100
------------------------------
ticker date assets
2 BB 05-31-2020 60
3 BB 08-31-2020 70
------------------------------
ticker date assets
4 CCC 06-30-2020 80
5 CCC 09-30-2020 90
------------------------------
Solution 2:
I think you need this,
import pandas as pd
df = pd.DataFrame({"ticker": ["A", "A", "BB", "BB", "CCC", "CCC", "A"], "date": ["04-30-2020", "07-30-2020", "05-31-2020", "08-31-2020", "06-30-2020", "09-30-2020", "10-31-2020"], "assets": ["40","50","60","70","80","90","100"]})
import sys
thismodule = sys.modules[name]
ls = df.ticker.unique().tolist()
for i in ls: setattr(thismodule, "df"+i, df[df.ticker==i])
display(dfA,dfBB,dfCCC)
Output
Note:
- If you need to refer to those df names in a loop, please use the following:
for i in ls: print(eval("df"+i))
- name is enclosed within double underscores on both sides(underscore underscore name underscore underscore)
Post a Comment for "How Do I Create Multiple Dataframes That Are Named Based On The Value Of Another Dataframe?"