2

Hello guys i just wanna create 100 dataframes.

just like a df1, df2, df3 .. df100.

How to do that by python for loop?

for i in range(100):
    some_value = df3.loc[df3.index[i], "sentence"]
    df"1" = df.loc[df['Text'] == some_value]

results = [df1, df2, ... df100]
final = pd.concat(results).drop_duplicates().reset_index(drop=True)
  • why not just put them in a list like `my_dfs = []` then append to the list `my_dfs.append(df.loc[df['Text'] == some_value])` Then when ever you want to access them you can access them like `my_dfs[0]` etc. Alternatly store them in a dict. `my_dfs = {}` then in your code `my_dfs[i] = df.loc[df['Text'] == some_value]` then access as `my_dfs[0]` – Chris Doyle May 10 '21 at 10:48

1 Answers1

3

OPTION 1

Just fill results without using variable names:

results = []
for i in range(100):
    some_value = df3.loc[df3.index[i], "sentence"]
    results.append(df.loc[df['Text'] == some_value])

final = pd.concat(results).drop_duplicates().reset_index(drop=True)

OPTION 2

Generate variable names from strings by using exec (I would not suggest doing it this way):

for i in range(100):
    some_value = df3.loc[df3.index[i], "sentence"]
    temp_df = df.loc[df['Text'] == some_value]
    exec("df{} = temp_df".format(i))

results = [df1, df2, ... df100]
final = pd.concat(results).drop_duplicates().reset_index(drop=True)

(The code may contain errors since I didn't compile it)

Daniel
  • 20,420
  • 10
  • 92
  • 149