1

Hi i want ask about python for loop logic. here is some data.

art1 = ["A",'B','C','D','E','F']
quote = ["",'A','A','C','C','A']
g2 = pd.DataFrame({"Article":art1,"Quote":quote})

A is quoted by B,C,F and C is quoted by D and E. So I wrote a for loop.

dic = []
dic2 = []
for i in range(0,len(art1)) :
   dic.append(art1[i])
   dic2.append("")
   for j in range(1,len(quote)):
       if art1[i] == quote[j]:
            dic2.append(art1[j])
            dic.append("")
g3 = pd.DataFrame(OrderedDict({"Article":dic,"Quote":dic2}))

enter image description here

But C quoted A. D and E quoted C. So I want to put D and E in category A. please...How do I code it?

This is the format I want.

judy
  • 17
  • 5
  • 1
    What is your *desired output*? While it's great you've shared your existing attempt, it seems like your method is inefficient and we can probably suggest a better algorithm. – jpp May 09 '18 at 08:40
  • @jpp The result is to categorize things related to A. D and E quoted C, but C is in the A category, so finding a way to group them together into the A category. – judy May 09 '18 at 08:51
  • 1
    @judy do you mean something like: `g2.groupby('Quote').Article.apply(set)` or `pd.get_dummies(g2, columns=['Article'], prefix='quoted_by').groupby('Quote').max()` - it'd help if you mocked up an example output dataframe that you're expecting - there's quite a few ways to "group" the data but it's not clear what you want. – Jon Clements May 09 '18 at 08:55
  • @JonClements I added the format I wanted. – judy May 09 '18 at 09:39
  • @JonClements You mentioned g2.groupby('Quote').Article.apply(set) seems to be a good way too. But when I add the data, the column is {A, G, H}.i want to like this {A} group {G}group {H}group. – judy May 09 '18 at 09:48
  • Ok, so this is a non-trivial recursive algorithm problem. Pandas almost certainly not the best solution here (I would go so far as to say it is a misuse). The best format is a nested dictionary or json structure. For some hints, I answered a [similar question](https://stackoverflow.com/a/50061161/9209546) recently. – jpp May 09 '18 at 10:24
  • @jpp Thank you very much for your opinion.I'll give it a try! – judy May 09 '18 at 12:32

0 Answers0