2024-09-27 02:24:29 发布
网友
我有一个具有多个同名列的数据帧,这会导致按列名访问时.loc失败
.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')]
在这种情况下,最好不要使用重复的列名
如果需要按位置选择列名称,请使用^{}:
df[df.iloc[:, 0] == 'abc'] df[df.iloc[:, 1].str.contains('xyz')]
在这种情况下,最好不要使用重复的列名
如果需要按位置选择列名称,请使用^{} :
相关问题 更多 >
编程相关推荐