0

I have the following list in Python:

list = [[1, (200, 45)], [2, (53, 543)], [3, (5, 5)], [4,(655, 6456464)],[5, (64564, 45)], [6, (6, 5445)], [7, (546, 46)], [8, (64, 645)]

I now want to save these into an Excel and then read them in. How can I do this? I have not found such a "simple" import with Google, mostly it is more complex imports of Excel files.

Thanks

Serpiente32

2
  • 1
    How do you want to format it? In 3 columns? Commented Jun 4, 2021 at 18:20
  • Yes that would be perfect :) Commented Jun 4, 2021 at 18:27

1 Answer 1

1

Here is a sample code:

import pandas as pd

list_ = [[1, (200, 45)], [2, (53, 543)], [3, (5, 5)], [4,(655, 6456464)],[5, (64564, 45)], [6, (6, 5445)], [7, (546, 46)], [8, (64, 645)]]
list_ = [[item[0], *item[1]] for item in list_]
# The line above just unpacks the tuple and makes every element a list of 3 numbers

pd.DataFrame(data=list_).to_excel("data.xlsx", index=False) # save in excel
# you now have an excel file in same folder with three columns

data = pd.read_excel("data.xlsx")  # read back from excel
print(data)

Output:

   0      1        2
0  1    200       45
1  2     53      543
2  3      5        5
3  4    655  6456464
4  5  64564       45
5  6      6     5445
6  7    546       46
7  8     64      645

Note: You should never use reserved keywords like list as label names. Can cause unexpected issues in your programs.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.