Reputation: 6260
I have a very unstructured folder, where a lot of files have no entries (just the row headers), but there is no data inside. I know that i can include them and they will not change anything, but the problem is that the headers are not the same everywhere, so every file includes some extra manual work for me.
Until now I now how to load all files in a specific folder with the following code:
import glob
path = r'C:/Users/...'
all_files = glob.glob(path+ "/*.csv")
li = []
for filename in all_files:
frame = pd.read_csv(filename, index_col=None, header=0, sep=';', encoding='utf-8', low_memory=False)
li.append(frame)
df = pd.concat(li, axis=0, ignore_index=True, sort=False)
How can I skip every file, which only has one row?
Upvotes: 0
Views: 368
Reputation: 71580
Modify this loop from:
for filename in all_files:
frame = pd.read_csv(filename, index_col=None, header=0, sep=';', encoding='utf-8', low_memory=False)
li.append(frame)
To:
for filename in all_files:
frame = pd.read_csv(filename, index_col=None, header=0, sep=';', encoding='utf-8', low_memory=False)
if len(frame) > 1:
li.append(frame)
That's what if
statements are for.
Upvotes: 1