根据条件使用列号和筛选行访问pandas中的列

2024-09-27 02:24:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个具有多个同名列的数据帧,这会导致按列名访问时.loc失败

df = pd.DataFrame([['abc', 'xyz'], ['abc', 'xyz'], ['xyz', 'abc']], columns=['max_speed', 'max_speed'])

我正在尝试本answer中提到的以下方法,但没有成功

df.loc[df.columns[0] == 'abc']

df.loc[df.columns[1].str.contains('xyz')]

Tags: columns数据方法answerdataframedflocmax

热门问题