带引号的列名

时间:2016-04-12 15:46:42

标签: python pandas

在eof上读取带有read_csv的csv文件后,我的列的名称带引号:" my_col"。

我无法访问它:

df['"my_col"'] or df['my_col'] 

怎么可以访问它?我唯一能想到的就是改名。我可以逃避"字符?

df.columns.tolist()
['\ufeff"COU"',
 'FAM',
  'CHI',
  'ER1',
  'ER2']

"COU","FAM","CHI","ER1","ER2","VAR","EMP","YEA","Unit Code","PowerCodeCode","Reference Period Code","Value","Flag Codes"
"AUS","SGL","0C","0P","NA","SA","EMP","2001",,"0",,9303,
"AUS","SGL","0C","0P","NA","SA","EMP","2002",,"0",,9594,
"AUS","SGL","0C","0P","NA","SA","EMP","2003",,"0",,9883,
"AUS","SGL","0C","0P","NA","SA","EMP","2004",,"0",,10119,
"AUS","SGL","0C","0P","NA","SA","EMP","2005",,"0",,10382,
"AUS","SGL","0C","0P","NA","SA","EMP","2006",,"0",,10676,
 "AUS","SGL","0C","0P","NA","SA","EMP","2007",,"0",,11032,

1 个答案:

答案 0 :(得分:0)

在您的案例中阅读csv文件:正如@EdChum在评论中所建议的那样,您需要通过encoding='utf-16'到read_csv

问题的下一部分:在引用dataFrame中的列时,您是否可以使用双引号字符(")?答案是肯定的。这是一个例子:

In [7]:

import pandas as pd
d = {
         '"one"': [1, 2, 3, 4, 5],
         '"two': [9, 8, 7, 6, 5]
    }
df = pd.DataFrame(d)

In [8]:
df['"one"']

Out[8]:
0    1
1    2
2    3
3    4
4    5
Name: "one", dtype: int64

In [9]:    
df['"two']

Out[9]:
0    9
1    8
2    7
3    6
4    5
Name: "two, dtype: int64