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
How to Validate a Url with a Regular Expression in Python
How to Execute Python File in Linux
Detecting When a Child Process Is Waiting for Input
How to Prevent Numbers Being Changed to Exponential Form in Python Matplotlib Figure
Running Selenium Webdriver with a Proxy in Python
Python: Problem with Raw_Input Reading a Number
How to Access "Static" Class Variables Within Methods in Python
What Is the Use of Join() in Python Threading
How Do Chained Assignments Work
Evenly Distributing N Points on a Sphere
Too Many Values to Unpack', Iterating Over a Dict. Key=>String, Value=>List
Curses-Like Library for Cross-Platform Console App in Python
Python3.6 Importerror: Cannot Import Name 'Main' Linux Rhel6
When Does Python Allocate New Memory for Identical Strings
Quick and Easy File Dialog in Python
Find Shortest Matches Between Two Strings