9870
9870

Reputation: 33

Pandas group daily text data into weekly with date index

Sample Dataset:

    Date         Text
2018-01-01      Apple
2018-01-01      Banana
2018-01-06      Cat
2018-01-08      Dog
2018-01-09      Elephant

I want my dataset to look like this:

Date              Text
2018-01-01        Apple Banana Cat
2019-01-08        Dog Elephant

Upvotes: 1

Views: 40

Answers (1)

Shaheer Akram
Shaheer Akram

Reputation: 765

try using the groupby function. here is a sample code that might help

import pandas as pd
import numpy as np

df = pd.DataFrame({
        'Date': ['2015-05-08', '2015-05-07', '2015-05-06', '2015-05-05', '2015-05-08', '2015-05-07', '2015-05-06', '2015-05-05'], 
        'Sym': ['aapl', 'aapl', 'aapl', 'aapl', 'aaww', 'aaww', 'aaww', 'aaww']
    })
print(df)

gives:

         Date   Sym
0  2015-05-08  aapl
1  2015-05-07  aapl
2  2015-05-06  aapl
3  2015-05-05  aapl
4  2015-05-08  aaww
5  2015-05-07  aaww
6  2015-05-06  aaww
7  2015-05-05  aaww

now if we groupby date

df = df.groupby(['Date']).apply(lambda x: list(np.unique(x)))
l = []
for i in df:
    i[1] =  i[1]+i[2]
    i.pop()
    l.append(i)

df = pd.DataFrame(l)
print(df)    

it gives the output:

            0         1
0  2015-05-05  aaplaaww
1  2015-05-06  aaplaaww
2  2015-05-07  aaplaaww
3  2015-05-08  aaplaaww

Upvotes: 1

Related Questions