Reputation: 73
I have an excel file with a data as shown below:
Col_Title1 | Col_Title2 | Col_Title3 | Label
Row11 | Row12 | Row13 | 1
Row21 | Row22 | Row23 | 2
Row31 | Row32 | Row33 | 3
Using pandas, I read this excel file like this:
import pandas as pd
df = pd.read_excel(FOLDER_DOWNLOAD_LOCATION + '1.xlsx', sheet_name='Sheet1')
Now, I wish to output the following list using the df
:
1. [[Row11, Row12, Row13], [Row21, Row22, Row23], [Row31, Row32, Row33]]
2. [1,2,3]
I don't know the efficient way or a good technique to attain those type of list outputs.
P.S. I'm a newbie in programming & sorry for asking this type of question.
Upvotes: 0
Views: 18393
Reputation: 164613
One way is to use slicing, assuming you know the order of your columns in advance.
df_values = df.values
# [['Row11' 'Row12' 'Row13' 1]
# ['Row21' 'Row22' 'Row23' 2]
# ['Row31' 'Row32' 'Row33' 3]]
Then slice as appropriate:
df_values[:, :-1].tolist()
# [['Row11', 'Row12', 'Row13'],
# ['Row21', 'Row22', 'Row23'],
# ['Row31', 'Row32', 'Row33']]
df_values[:, -1].tolist()
# [1, 2, 3]
Upvotes: 3
Reputation: 862396
I believe you need remove Label
column, convert to numpy array by values
and then to list
:
a = df.drop('Label', 1).values.tolist()
print (a)
[['Row11', 'Row12', 'Row13'], ['Row21', 'Row22', 'Row23'], ['Row31', 'Row32', 'Row33']]
b = df['Label'].values.tolist()
print (b)
[1, 2, 3]
Upvotes: 3