How to Convert a Pandas Series or Index to a Numpy Array

Converting pandas index to numpy array. Python

You can just do df.index.values:

df = pd.DataFrame(index=['a', 'b', 'c'])

df.index.values
# array(['a', 'b', 'c'], dtype=object)

How do I convert a Pandas series or index to a NumPy array?

To get a NumPy array, you should use the values attribute:

In [1]: df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}, index=['a', 'b', 'c']); df
A B
a 1 4
b 2 5
c 3 6

In [2]: df.index.values
Out[2]: array(['a', 'b', 'c'], dtype=object)

This accesses how the data is already stored, so there isn't any need for a conversion.

Note: This attribute is also available for many other pandas objects.

In [3]: df['A'].values
Out[3]: Out[16]: array([1, 2, 3])

To get the index as a list, call tolist:

In [4]: df.index.tolist()
Out[4]: ['a', 'b', 'c']

And similarly, for columns.

How to convert pandas series with numpy array values to dataframe

You can try of using pandas Dataframe from records

pd.DataFrame.from_records(series.values,index=series.index)

Out:

    0   1   2   3
file1 1 2 3 4
file2 5 6 7 8
file3 9 10 11 12

Python: Convert a pandas Series into an array and keep the index

Something like the one-liner below could work:

a = list(map(list, df["PredictedCluster"].value_counts().items()))

Pandas Series to_numpy() preserve names of indices

You want ser.iteritems() (or list(ser.iteritems()) if you directly need the results as a list), not ser.to_numpy().

How to convert a pandas dataframe to NumPy array

It seems that you want to convert the DataFrame into a 1D array (this should be clear in the post).

First, convert the DataFrame to a 2D numpy array using DataFrame.to_numpy (using DataFrame.values is discouraged) and then use ndarray.ravel or ndarray.flatten to flatten the array.

arr = df.to_numpy().ravel()

How can I convert a Pandas Series to a Numpy Array and maintain order?

Assuming the indices are always the same (but not necessarily occurring in the same order), you can use .sort_index() on the series. This will ensure the series is consistently ordered by its index each time.

https://pandas.pydata.org/pandas-docs/version/0.17.0/generated/pandas.Series.sort_index.html

convert pandas series AND dataframe objects to a numpy array

IIUC, you may try numpy transpose and reshape

df.values.T.reshape(-1,  int(dimension_len), int(dimension_len))

Out[30]:
array([[[ 0., 1., 2.],
[ 3., 4., 5.],
[ 6., 7., 8.]],

[[nan, -2., nan],
[ 2., nan, nan],
[nan, nan, nan]],

[[nan, nan, 4.],
[nan, nan, 3.],
[-4., -3., nan]]])


Related Topics



Leave a reply



Submit