Reputation: 2697
Lets assume I have a Pandas Dataframe like this:
C1 C2 C3
0 1 B v
1 5 D i
2 1 B iii
3 3 C iv
Where all possible values of C1, C2, C3 are
C1 = [1,2,3,4,5]
C2 = ['A','B','C','D','E']
C3 = ['i','ii','iii','iv','v']
The ask is to print new rows to exhaust all possible combinations of C1, C2, C3 that are not already in the existing dataframe.
Is there a better way than to have 3 nested loops ranging all the values of C1, C2, C3 and comparing each combination to the existing list?
Upvotes: 0
Views: 49
Reputation: 3310
series_1 = pd.Series(C1)
new_data_frame['C1'] = series_1.values
series_2 = pd.Series(C2)
new_data_frame['C2'] = series_2.values
series_3 = pd.Series(C3)
new_data_frame['C3'] = series_3.values
for index, row in new_data_frame.iterrows():
if existing_dataframe[existing_dataframe == row].shape[0] == 0:
existing_dataframe.appen(row)
Upvotes: 0
Reputation: 3185
You could try something like this,
C1 = [1, 2, 3, 4, 5]
C2 = ['A', 'B', 'C', 'D', 'E']
C3 = ['i', 'ii', 'iii', 'iv', 'v']
existing = ((1, 'B', 'v'), (5, 'D', 'i'), (1, 'B', 'iii'), (3, 'C', 'iv'))
import itertools
result = [i for i in itertools.product(C1, C2, C3) if i not in existing]
Upvotes: 1