2

How can I convert 1 column and the index of a Pandas dataframe with several columns to a Numpy array with the dates lining up with the correct column value from the dataframe?

There are a few issues here with data type and its driving my nuts trying to get both the index and the column out and into the one array!!

Help would be much appreciated!

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
cwse
  • 584
  • 2
  • 10
  • 20
  • Can you clarify your question as it's unclear what you really want, ideally with sample data, your code and desired output – EdChum Oct 03 '16 at 13:25
  • Hi @EdChum, after much difficulty I have converted the dates to values instead as I couldnt see another way... so I now want to join the values(prevously dates) to the column values. Can I join a 1D array of numpy values to another 1D array of Numpy float values, such that each value lines up with each value i.e. values 1 = [1,2,3,4] values 2 = [7,8,9,10] joint = [[1,7],[2,8],[3,9],[4,10]] Whenever I try and do it with concatenate it sticks them at either end, but I need the values to line up! Thanks! – cwse Oct 03 '16 at 13:33

2 Answers2

4

If A is dataframe and col the column:

import pandas as pd output = pd.np.column_stack((A.index.values, A.col.values))

Saurabh7
  • 710
  • 1
  • 5
  • 18
  • +1. For other users: As the question is about a date index (e.g. `datetime64[ns]`), it's likely there is no common dtype between `index` and `col`, and `index` needs some conversion. Also `.values` has been superseded in the meantime by `.to_numpy()`. See [this question](https://stackoverflow.com/q/31789160/774575). – mins Nov 14 '21 at 09:08
1

IIUC you need values:

start = pd.to_datetime('2015-02-24')
rng = pd.date_range(start, periods=5)

df = pd.DataFrame({'a': range(5), 'b':list('ABCDE')}, index=rng)  
print (df)
            a  b
2015-02-24  0  A
2015-02-25  1  B
2015-02-26  2  C
2015-02-27  3  D
2015-02-28  4  E

print (df.values)
[[0 'A']
 [1 'B']
 [2 'C']
 [3 'D']
 [4 'E']]

if need index values also first convert datetime to string values in index and then use reset_index for converting index to column:

df.index = df.index.astype(str)
print (df.reset_index().values)
[['2015-02-24' 0 'A']
 ['2015-02-25' 1 'B']
 ['2015-02-26' 2 'C']
 ['2015-02-27' 3 'D']
 ['2015-02-28' 4 'E']]
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • thanks @jezrael but my values are 'date' format, and when i reset_index they some how turn into strings? I tried this approach.. I also tried converting the dates to floats and then joining back onto the column values. How could I make this work? Can I join a 1D array of numpy values to another 1D array of Numpy float values, such that each value lines up with each value i.e. values 1 = [1,2,3,4] values 2 = [7,8,9,10] joint = [[1,7],[2,8],[3,9],[4,10]] Whenever I try and do it with concatenate it sticks them at either end, but I need the values to line up! Thanks! – cwse Oct 03 '16 at 13:32
  • I edit answer with `string` index and if need concanecate `numpy array`, use `(np.vstack((np.array([1,2,3,4]),np.array([7,8,9,10]))).T)` – jezrael Oct 03 '16 at 13:43