I'm brand new to Python so I hope this makes sense:
I've created a list from file names I've pulled using the os module.
The file names look like '### Name 00.00', so there are integers, words, and floats all mixed into single strings in my list. It looks like this:
files_list = ['### File1 00.00','### File2 00.00','### File3 00.00']
I really need it to look something like:
files_list = [['###','File1','00.00'],['###','File2','00.00'],['###','File3','00.00']]
That way I can create a dataframe with the columns "Invoice #", "Vendor" , "Amount".
However, all I've been able to figure is to create a nested list with each string, so:
files_list = [files[i:i 1] for i in range(0, len(files), 1)]
#which returns this format:
files_list = [[### File1 00.00],[### File2 00.00],[### File3 00.00]]
I'm sure there's a super simple solution, but I just don't know exactly how to ask the question in google.
TYIA
CodePudding user response:
import pandas as pd
labels = ["Invoice #", "Vendor", "Amount"]
files_list = ["### File1 00.00", "### File2 00.00", "### File3 00.00"]
pd.DataFrame.from_records(dict(zip(labels, x.split(" "))) for x in files_list)
result:
Invoice # Vendor Amount
0 ### File1 00.00
1 ### File2 00.00
2 ### File3 00.00
CodePudding user response:
You can use pd.Series.str.split
:
pd.Series(files_list).str.split().tolist()
produces:
[['###', 'File1', '00.00'],
['###', 'File2', '00.00'],
['###', 'File3', '00.00']]