我得到了以下数据帧,其中包含一个空行,因为所选插槽(即时间(缺少值:
slot tempo
10 7--8 132.559556
slot tempo
8 7--8 135.0565
slot tempo
9 7--8 125.582778
slot tempo
7 7--8 117.038667
slot tempo
9 7--8 135.946333
Empty DataFrame
Columns: [slot, tempo]
Index: []
slot tempo
2 7--8 123.476571
slot tempo
3 7--8 125.724286
slot tempo
2 7--8 139.503
slot tempo
2 7--8 140.977429
slot tempo
1 7--8 135.035875
slot tempo
1 7--8 120.741556
我用来获得这个df的代码是:
path = os.getcwd()
csv_files = glob.glob(os.path.join(path, "*.csv"))
for f in csv_files:
dfDay = pd.read_csv(f, encoding = "ISO-8859-1", sep = ';')
dfSlotMean = dfDay.groupby('slot', as_index=False)['tempo'].mean()
slotMorningMean = dfSlotMean[dfSlotMean.slot == '7--8']
# print(type(slotMorningMean))
print((slotMorningMean))
如何删除此空数据帧?我已经试过dropna了,但没有成功。
您可以使用pandas.DataFrame
的.empty
属性,如果它为true,则使用continue
循环控制跳到下一个,如下所示
path = os.getcwd()
csv_files = glob.glob(os.path.join(path, "*.csv"))
for f in csv_files:
dfDay = pd.read_csv(f, encoding = "ISO-8859-1", sep = ';')
dfSlotMean = dfDay.groupby('slot', as_index=False)['tempo'].mean()
slotMorningMean = dfSlotMean[dfSlotMean.slot == '7--8']
# print(type(slotMorningMean))
if slotMorningMean.empty:
continue
print((slotMorningMean))