import pandas as pd
import matplotlib.pyplot as plt
# 假设df是你的DataFrame
df = pd.DataFrame({'x': [1, 2, 3, 4], 'y': [1, 4, 9, 16]})
# 正确的单列索引方式
x = df['x']
y = df['y']
# 使用x和y作为plot的参数
plt.plot(x, y)
plt.show()
使用3.8的版本,上述代码plot绘图时报错:
ValueError: Multi-dimensional indexing (e.g. obj[:, None]
) is no longer supported. Convert to a numpy array before indexing instead.
不支持多维索引(DateFrame有行索引、列索引),需要将dataframe(例:df[‘x’])转成数组
需要将
df[‘x’] 、df[‘y’]转成numpy数组
方法一:修改代码
x = np.array(df['x'])
y = np.array(df['y'])
plt.plot(x, y)
plt.show()
方法二:3.6版本不会报错
1、在Anaconda中创建新环境
conda create -n python36 python=36
2、激活新环境
conda activate python36