'How to add content of a txt file in a dataframe in python
I have a folder containing two sub folders names Source and Suspicious, they contain 200 txt files each, i want to create a dataframe in python where column 1 shows filenames of Source .txt files and column two shows filenames of Suspicious .txt files and column 3 shows the content of the source .txt files and similarly column 4 shows content of suspicious .txt files.
I have created two columns of the filenames, but I don't how how to add content of the files in a dataframe. How do I create column three and column four showing the content of the files. Dataframe image
I want something like this dataframe where there is a separate column for text (content) of the file. Example of the dataframe containing both filename and text.
Solution 1:[1]
How about something like this?
d = 'PAN PC 2011'
files = {f: pd.Series([e.path for e in os.scandir(os.path.join(d, f))]) for f in os.listdir(d)}
df = pd.DataFrame(files)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | richardec |
