0

i have a dataset structure like this :

| id | age | name | income |
----------------------------
|  0 |  9  |  AA  | 300    |
|  1 |  6  |  ZZ  | 100    |

and how to extract age and income and then combining again and convert those data into multidimentional list like this :

[
  [9, 300], 
  [6, 100]
]

i've tried a concate from pandas documentation, but it goes wrong when i converted it into list. my code is :

dataframe = pd.read_csv('my.csv')
data = pd.concat([dataset['age'], dataset['income']], axis=1)

everything is fine if i type print(data), the result is dataframe is already merged. But if i type print(list(data)) the result i received is only header's name appear.

maybe you guys know how to solve my problem, any help will be appreciated

thanks

AMC
  • 2,642
  • 7
  • 13
  • 35
DarkCode999
  • 182
  • 1
  • 1
  • 11
  • ```df.filter(['age','income']).to_numpy()``` ? – sammywemmy May 01 '20 at 09:16
  • 1
    @sammywemmy thanks for the answer & your reply sam, i've tried your suggestion and it works, but i found another simple way to solve my case. :D – DarkCode999 May 01 '20 at 09:27
  • Please provide a [mcve], and clarify what exactly your question is. Why do you want a list, and what kind of list? – AMC May 01 '20 at 10:19
  • Does this answer your question? [Pandas DataFrame to List of Lists](https://stackoverflow.com/questions/28006793/pandas-dataframe-to-list-of-lists) – AMC May 01 '20 at 10:22

1 Answers1

2

I'm not sure to understand why using merge.

Assuming you have a dataframe like

#    id  age name  income
# 0   0    9   AA     300
# 1   1    6   ZZ     100

You can try values followed by numpy.ndarray.tolist:

df[["age", "income"]].values.tolist()
# [[9, 300], [6, 100]]
print(type(df[["age", "income"]].values.tolist()))
# <class 'list' >

values return a numpy array:

print(type(df[["age", "income"]].values))
# <class 'numpy.ndarray'>
print(df[["age", "income"]].values)
# [[  9 300]
#  [  6 100]]
Alexandre B.
  • 5,387
  • 2
  • 17
  • 40