Reputation: 1888
I have a pandas dataFrame of fruits::
df = pd.read_csv(newfile, header=None)
df
0 1 2 3 4 5 6 7
0 Apple Bananas Fig Elderberry Cherry Honeydew NaN NaN
1 Bananas Cherry Dragon Elderberry NaN NaN NaN NaN
2 Cherry Grape NaN NaN NaN NaN NaN NaN
3 Dragon NaN Apple Bananas Cherry Elderberry NaN NaN
4 Elderberry Apple Bananas Fig Grape NaN NaN NaN
5 Fig Cherry Honeydew Apple NaN NaN NaN NaN
6 Grape NaN NaN NaN NaN NaN NaN NaN
7 Honeydew Grape Fig Elderberry Dragon Cherry Bananas Apple
And I'm trying to find "fruit pairings", e.g. in the first row, Apple and Fig are a pair, and 6th row Fig and Apple. Likewise for Apple-Elderberry and Elderberry-Apple, but not Apple and Bananas (there are no Apples in the row starting with Bananas).
I've got the following code working, and that does this::
fruits = df[0]
stock = df.drop(0, axis=1)
for i in range(len(fruits)):
string1 = str(fruits[i])
full_line = (stock.iloc[i])
line = np.array(full_line.dropna(axis=0))
if len(line) > 0 :
for j in range(len(stock)):
iind = (fruits[fruits == line[j]].index[0])
this_line = stock.iloc[iind]
logic_out = this_line.str.match(string1)
print(logic_out)
BUT!! (1) It breaks at the fruits == line[j] due the Pandas Series being case sensitive and (2) the boolean out put is a mixture of True's, Falses and NaNs. Ideally, I just want to count the Trues. Any and all help v. much appreciated!!
Upvotes: 0
Views: 198
Reputation: 294358
I'm going to use set logic, pandas stacking, and numpy broadcasting
f = lambda x: x.title() if isinstance(x, str) else x
s = df.applymap(f).set_index('0').rename_axis(None).stack().groupby(level=0).apply(set)
f = s.index
p = s.values
one_way = (p[:, None] & [{x} for x in f]).astype(bool)
[(f[i], f[j]) for i, j in zip(*np.where(one_way & one_way.T))]
[('Apple', 'Elderberry'),
('Apple', 'Fig'),
('Apple', 'Honeydew'),
('Bananas', 'Dragon'),
('Bananas', 'Elderberry'),
('Dragon', 'Bananas'),
('Elderberry', 'Apple'),
('Elderberry', 'Bananas'),
('Fig', 'Apple'),
('Fig', 'Honeydew'),
('Honeydew', 'Apple'),
('Honeydew', 'Fig')]
Upvotes: 1