Home > OS >  Make event N rows of a Dataframe into a column Aggregated by date in pandas?
Make event N rows of a Dataframe into a column Aggregated by date in pandas?

Time:09-30

I have a dataframe like this

title timestamp
String one 2016-12-02
String two 2016-12-02
... ...
String twenty-five 2016-12-02
String twenty-six 2016-12-03
String twenty-seven 2016-12-03
... ...
String fifty 2016-12-03

And I want to change it into a dataframe like this

timestamp top 1 top 2 ... top 25
2016-12-02 String one String two ... String twenty-five
2016-12-03 String twenty-six String twenty-seven ... String fifty

Basically i'm trying to make every 25 rows to become columns consecutively and i'm not sure how big the dataset will be but it's going to have atleast a few years as date.

I know i'm suppose to use reshape or melt but I've been unsuccessful till now. Any help in how to tackle this?

CodePudding user response:

Here is an idea, where column A is your dates and column D is your strings

df = pd.DataFrame({"A": ["foo", "foo", "foo", "foo", "foo",
                         "bar", "bar", "bar", "bar"],
                   "D": ["1", "this text", "text other", "What", "migrate", "Data", "5", "6 saucisses", "Okayy"]
                   })
# Join with a character very specific, that you won't find in your rows
# Something like '#######'
df = df.pivot_table(values='D', index=['A'], aggfunc=lambda x: '#'.join(x))

df_test = df.D.str.split(pat='#',expand=True,)


  • Related