Python 3.x [columns]错误中没有[Index([';a';,';c';],dtype=';object';])
我有一个主数据框,希望创建一个具有特定列的子数据框:Python 3.x [columns]错误中没有[Index([';a';,';c';],dtype=';object';]),python-3.x,pandas,Python 3.x,Pandas,我有一个主数据框,希望创建一个具有特定列的子数据框: df_main= [a,b,c,d] [1,3,6,0] 当我想挑选特定的列并创建一个新列时,它会抛出一个丑陋的错误: df_new= df.loc[:, ['a','c']] df_new.head() Out:None of [Index(['a', 'c'], dtype='object')] are in the [columns] 这里有什么问题?如果我理解正确: import pandas
df_main=
[a,b,c,d]
[1,3,6,0]
当我想挑选特定的列并创建一个新列时,它会抛出一个丑陋的错误:
df_new=
df.loc[:, ['a','c']]
df_new.head()
Out:None of [Index(['a', 'c'], dtype='object')] are in the [columns]
这里有什么问题?如果我理解正确:
import pandas as pd
df = pd.DataFrame({'a':[1], 'b':[3], 'c':[6], 'd':[0]})
df_new = df.loc[:, ['a','c']]
df_新:
a c
0 1 6
什么是
df
?您定义了df_main
…什么是print(df.columns)
您必须保持最小的、可复制的和可读的代码,因此请向我们展示格式正确的数据帧结构以及预期的输出,以获得答案。请检查列标题中的空格。要修复此问题,请使用条带
df.columns=df.columns.str.strip()