使用包含空单元格的数据框标记数据点

labeling data points with dataframe including empty cells

我有一个 Excel sheet 这样的:

A    B    C    D
3    1    2    8
4    2    2    8
5    3    2    9
          2    9
6    4    2    7

现在我试图在 'C' 上绘制 'B' 并用 'A' 的条目标记数据点。它应该向我显示带有相应标签的点 1/2、2/2、3/2 和 4/2。

import matplotlib.pyplot as plt
import pandas as pd
import os

df = pd.read_excel(os.path.join(os.path.dirname(__file__), "./Datenbank/Test.xlsx"))

fig, ax = plt.subplots()
df.plot('B', 'C', kind='scatter', ax=ax)
df[['B','C','A']].apply(lambda x: ax.text(*x),axis=1);

plt.show()

不幸的是我得到这个:

错误:

ValueError: posx and posy should be finite values

如您所见,它没有标记最后一个数据点。我知道这是因为 sheet 中的空单元格,但我无法避免它们。只是这个位置没有测量数据。 我已经在这里搜索了解决方案: Annotate data points while plotting from Pandas DataFrame 但这并没有解决我的问题。

那么,有没有办法仍然标记最后一个数据点?

P.S.: excel sheet 只是一个例子。所以请记住,实际上在不同的位置有很多空单元格。

您可以在绘制之前简单地将 df 中的无效数据行丢弃

df = df[df['B'].notnull()]