1

I have several Pandas Series of unique strings:

    First Series
    P0A8V2
    P36683
    P15254

    Second Series
    P09831
    P0AFG8

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?

UPDATE:

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?

Merlin
  • 24,552
  • 41
  • 131
  • 206
Benni
  • 795
  • 2
  • 7
  • 20

2 Answers2

3

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
ayhan
  • 70,170
  • 20
  • 182
  • 203
  • Thank you, my file seems to be right. But I get the error: IOError: [Errno 22] invalid mode ('w') or filename: 'Cluster_Network.csv' – Benni Aug 07 '16 at 18:38
  • Can it be a problem about the path? http://stackoverflow.com/questions/15141761/region-ioerror-errno-22-invalid-mode-w-or-filename – ayhan Aug 07 '16 at 18:44
1

Another way to do this would be to use convert the Series to list and then use itertools.combinations to get the desired results... like so,

import pandas as pd
s1  = pd.Series(['a', 'b', 'c'])
s2  = pd.Series(['d', 'e'])

import itertools
s= s1.tolist()
s.extend(s2.tolist())
open('test.txt','w').writelines(["%s\t%s\n" % (item[0], item[1])  for item in list(itertools.combinations(s,2))])
nitin
  • 7,234
  • 11
  • 39
  • 53