ValueError: Multi-dimensional indexing is no longer supported. Convert to a numpy array before index

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值