Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x [columns]错误中没有[Index([';a';,';c';],dtype=';object';])_Python 3.x_Pandas - Fatal编程技术网

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()