2

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? 
2
  • 3
    yes, but actions like save and read are messier. Commented Jun 18, 2020 at 2: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. Commented Jun 18, 2020 at 2:28

2 Answers 2

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']})
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.