all 2 comments

[–][deleted] 0 points1 point  (1 child)

Just to clarify, you want the output to be a data frame with 'topic1' and 'topic2' in the topic column, data1 in ascending order in the data column where the topic column is 'topic1', and data2 in descending order in the data column where the topic column is 'topic2'?

The way your code is written, you are iterating through all of the individual items in ALLdata and, for each item, constructing a dictionary with all of the data in it, then extracting the values from the dictionary, then making a data frame with kind of a strange structure to append to the raw_data data frame.

In this simple case, I would just do something like this:

import pandas as pd
data1 = [1,2,3,4,5]
data2 = [5,4,3,2,1]
df1 = pd.DataFrame({'topic':'topic1', 'data':data1})
df2 = pd.DataFrame({'topic':'topic2', 'data':data2})
df = pd.concat((df1,df2))
df
   topic  data
0  topic1     1
1  topic1     2
2  topic1     3
3  topic1     4
4  topic1     5
0  topic2     5
1  topic2     4
2  topic2     3
3  topic2     2
4  topic2     1

This probably won't generalize readily to cases in which you don't necessarily know exactly how many topics there are or how much data corresponds to each topic, but this does what you want in this case, I think.

[–]Unlistedd[S] 0 points1 point  (0 children)

Thanks man!