Python Forum
[Numpy] How to store different data type in one numpy array?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Numpy] How to store different data type in one numpy array?
#3
Example like this.
>>> import numpy as np
>>>
>>> dtype = [('date', 'datetime64[D]'), ('value', 'float64'), ('code', 'U3')]
>>> n = np.array([('2024-03-22', 71.0, 'ceh'), ('2024-03-23', 63.0, 'abc')], dtype=dtype)
>>> n
array([('2024-03-22', 71., 'ceh'), ('2024-03-23', 63., 'abc')],
      dtype=[('date', '<M8[D]'), ('value', '<f8'), ('code', '<U3')])
String of up to 3 characters <U3.
>>> n = np.array([('2024-03-22', 71.0, 'cehar'), ('2024-03-23', 63.0, 'abchhhhhhhhhh')], dtype=dtype)
>>> n
array([('2024-03-22', 71., 'ceh'), ('2024-03-23', 63., 'abc')],
      dtype=[('date', '<M8[D]'), ('value', '<f8'), ('code', '<U3')])
To Pandas dataFrame,as Panda i build on NumPy in bottom it seamlessly transfer over.
>>> import pandas as pd
>>> 
>>> df = pd.DataFrame(n)
>>> df
        date  value code
0 2024-03-22   71.0  ceh
1 2024-03-23   63.0  abc
Reply


Messages In This Thread
RE: [Numpy] How to store different data type in one numpy array? - by snippsat - Mar-24-2024, 11:25 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: '>' not supported between instances of 'numpy.str_' and 'int' Anouar 0 228 Apr-26-2024, 09:34 AM
Last Post: Anouar
  Bitwise Operations in numpy Sowmya 3 410 Apr-03-2024, 02:51 PM
Last Post: deanhystad
  [Numpy] Load date/time from .txt to 'datetime64' type. water 4 790 Mar-01-2024, 11:16 PM
Last Post: Gribouillis
  numpy.ufunc - Arguments missunderstand MarioBruza 0 880 Jan-11-2023, 05:03 AM
Last Post: MarioBruza
  reshaping 2D numpy array paul18fr 3 1,066 Jan-03-2023, 06:45 PM
Last Post: paul18fr
  Pandas dataframes and numpy arrays bytecrunch 1 1,380 Oct-11-2022, 08:08 PM
Last Post: Larz60+
  Numpy returns "TypeError: unsupported operand type(s) for *: 'numpy.ufunc' and 'int'" kalle 2 2,708 Jul-19-2022, 06:31 AM
Last Post: paul18fr
Question about Numpy indexing. water 1 1,505 Jan-18-2022, 09:52 PM
Last Post: paul18fr
  numpy masking/filtering nilamo 3 3,596 Dec-04-2021, 10:28 PM
Last Post: nilamo
  Data Science - "key of type tuple not found and not a MultiIndex" priyanshuaggarwal 0 5,337 Nov-07-2021, 11:22 PM
Last Post: priyanshuaggarwal

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020