1

Is it possible for there to be dataframe where (for example) there is a column called "data", and each element in the column was a numpy array?

| Data              | Time          |
| [1, 2, 3, ... 10] | June 12, 2020 |
| [11, 12, ..., 20] | June 13, 2020 |

If so, how do you create a dataframe in this format? 
  • 3
    yes, but actions like save and read are messier. – hpaulj Jun 18 '20 at 02:12
  • You can create a column which stores arbitrary Python objects and numpy arrays are Python objects. But processing the data in the column efficiently (better than a Python for-loop) will be a problem then. – Michael Butscher Jun 18 '20 at 02:28

2 Answers2

2

Not sure you want to do it this way, but it works.

import pandas as pd
import numpy as np
df = pd.DataFrame({'Data': [np.array([1, 2, 3, 10]), np.array([11,12,13,20])], 'Time' : ['June 12, 2020', 'June 13, 2020']})
print (df)

Output:

               Data           Time
0     [1, 2, 3, 10]  June 12, 2020
1  [11, 12, 13, 20]  June 13, 2020

You can also do it with lists:

df = pd.DataFrame({'Data': [[1, 2, 3, 10], [11,12,13,20]], 'Time' : ['June 12, 2020', 'June 13, 2020']})
LevB
  • 925
  • 6
  • 10
0

Yes you can, follow this question. It's useful when you data grouped by date, indexes, etc. Because you compress some rows but in terms of pandas operations maybe it isn't that efficient. Maybe you will prefer to use groupby() method and then apply operations.

santiagoNublado
  • 322
  • 3
  • 11