Benni Benni - 2 months ago 5x
Python Question

Python: Write all combinations of a pd.Series in a text file

I have several Pandas Series of unique strings:

First Series

Second Series

I want to write a textfile that looks like this (tab seperator):

P0A8V2 P36683
P0A8V2 P15254
P36683 P15254
P09831 P0AFG8

So in a Series, every object is combined with every other exactly once. The order doesn´t matter. Then the next Series starts right away.

Is there a easy way to do this?


The Strings are in the index of a DataFrame. I access them with df.index.values. The DataFrames are in df_list.

def Cluster_Network(df_list):
combi_list = []
for cluster in df_list:
combi_list.append(tuple(itertools.combinations(cluster.index.values, 2)))
return combi_list

I get a list of tuples with the pairs in it.

[('P77717', 'P10378'),
('P18393', 'P77444'),
('P18393', 'P0AD44'),
('P18393', 'P10378'),
('P77444', 'P0AD44'),
('P77444', 'P10378'),
('P0AD44', 'P10378')),
(('P77562', 'P41039'),)]

How can I write the textfile out of that list?


Looks like you are almost there.

combi_list = []
for cluster in df_list:
    combi_list.append(pd.DataFrame(list(itertools.combinations(cluster.index, 2))))
result_df = pd.concat(combi_list, ignore_index=True)
result_df.to_csv(filename, sep='\t', index=False, header=False) 

This would generate a file like this:

P0A8V2  P36683
P0A8V2  P15254
P36683  P15254
P09831  P0AFG8