I don't know what problem you have because you didn't show any error message, and you didn't create minimal working code which we could test.
I created my own minimal working code with small example data and all works for me.
But in inner for-loop I group all tables with the same fruit (on list group), and after for-loop I concate it pd.concat(group) and I append to empty list with results results.append(pd.concat(group)) instead of using index to replace items.
And this allows me to make all more readable without range() and without indexes.
import pandas as pd
multiple_fruit = ["apple", "banana", "lemon", "grape", "blueberry", "lime"]
data_tables = [
pd.DataFrame({"A": [1], "B": [1], "C": [1], "D": [1], "Name": ["apple"]}),
pd.DataFrame({"A": [2], "B": [2], "C": [2], "D": [2], "Name": ["banana"]}),
pd.DataFrame({"A": [3], "B": [3], "C": [3], "D": [3], "Name": ["lemon"]}),
pd.DataFrame({"A": [4], "B": [4], "C": [4], "D": [4], "Name": ["grape"]}),
pd.DataFrame({"A": [5], "B": [5], "C": [5], "D": [5], "Name": ["blueberry"]}),
pd.DataFrame({"A": [6], "B": [6], "C": [6], "D": [6], "Name": ["lime"]}),
pd.DataFrame({"A": [7], "B": [7], "C": [7], "D": [7], "Name": ["apple"]}),
pd.DataFrame({"A": [8], "B": [8], "C": [8], "D": [8], "Name": ["banana"]}),
pd.DataFrame({"A": [9], "B": [9], "C": [9], "D": [9], "Name": ["lemon"]}),
pd.DataFrame({"A": [0], "B": [0], "C": [0], "D": [0], "Name": ["apple"]}),
]
# --- grouping and concatenating ---
results = []
for name in multiple_fruit:
group = []
print(f"--- grouping: {name} ---")
for table in data_tables:
if table.loc[0, "Name"] == name:
print(table.loc[0, "Name"])
group.append(table)
print("number of tables in group:", len(group))
# results.append(pd.concat(group).reset_index(drop=True))
results.append(pd.concat(group, ignore_index=True))
# --- display results ---
for item in results:
print("--- result ---")
print(item)
Result:
--- grouping: apple ---
apple
apple
apple
number of tables in group: 3
--- grouping: banana ---
banana
banana
number of tables in group: 2
--- grouping: lemon ---
lemon
lemon
number of tables in group: 2
--- grouping: grape ---
grape
number of tables in group: 1
--- grouping: blueberry ---
blueberry
number of tables in group: 1
--- grouping: lime ---
lime
number of tables in group: 1
--- result ---
A B C D Name
0 1 1 1 1 apple
1 7 7 7 7 apple
2 0 0 0 0 apple
--- result ---
A B C D Name
0 2 2 2 2 banana
1 8 8 8 8 banana
--- result ---
A B C D Name
0 3 3 3 3 lemon
1 9 9 9 9 lemon
--- result ---
A B C D Name
0 4 4 4 4 grape
--- result ---
A B C D Name
0 5 5 5 5 blueberry
--- result ---
A B C D Name
0 6 6 6 6 lime
To make less loop iterations it could first use one loop to group data_tables in dict {"apple": [table, table, ...], "banana": ...} and later run other loop to concat groups and append to results.
import pandas as pd
multiple_fruit = ["apple", "banana", "lemon", "grape", "blueberry", "lime"]
data_tables = [
pd.DataFrame({"A": [1], "B": [1], "C": [1], "D": [1], "Name": ["apple"]}),
pd.DataFrame({"A": [2], "B": [2], "C": [2], "D": [2], "Name": ["banana"]}),
pd.DataFrame({"A": [3], "B": [3], "C": [3], "D": [3], "Name": ["lemon"]}),
pd.DataFrame({"A": [4], "B": [4], "C": [4], "D": [4], "Name": ["grape"]}),
pd.DataFrame({"A": [5], "B": [5], "C": [5], "D": [5], "Name": ["blueberry"]}),
pd.DataFrame({"A": [6], "B": [6], "C": [6], "D": [6], "Name": ["lime"]}),
pd.DataFrame({"A": [7], "B": [7], "C": [7], "D": [7], "Name": ["apple"]}),
pd.DataFrame({"A": [8], "B": [8], "C": [8], "D": [8], "Name": ["banana"]}),
pd.DataFrame({"A": [9], "B": [9], "C": [9], "D": [9], "Name": ["lemon"]}),
pd.DataFrame({"A": [0], "B": [0], "C": [0], "D": [0], "Name": ["apple"]}),
]
# --- grouping ---
all_groups = {}
for table in data_tables:
name = table.loc[0, "Name"]
print(name)
if name not in all_groups:
all_groups[name] = []
all_groups[name].append(table)
# --- concatenating ---
results = []
for name in multiple_fruit:
group = all_groups[name]
print(f"number of tables in group {name}:", len(group))
# results.append(pd.concat(group).reset_index(drop=True))
results.append(pd.concat(group, ignore_index=True))
# --- display results ---
for item in results:
print("--- result ---")
print(item)
minimal working codewith small example data, and result which you expect.DataTables[j]to list (ie.group) and afterfor j- loop concat all of thempd.concat(group)andappend()toConcat_Dup_Fruit. And this can work even with names which have only oneData_Table