今天,我遇到了这个dict方法get,它给出了字典中的一个键,返回了相关的值.
这个功能用于什么目的?如果我想找到与字典中的键相关联的值,我可以这样做dict[key],它返回相同的东西:
dictionary = {"Name": "Harry", "Age": 17}
dictionary["Name"]
dictionary.get("Name")
Run Code Online (Sandbox Code Playgroud) df = pd.DataFrame([[1,2,3], [10,20,30], [100,200,300]])
df.columns = pd.MultiIndex.from_tuples((("a", "b"), ("a", "c"), ("d", "f")))
df
Run Code Online (Sandbox Code Playgroud)
回报
a d
b c f
0 1 2 3
1 10 20 30
2 100 200 300
Run Code Online (Sandbox Code Playgroud)
和
df.columns.levels[1]
Run Code Online (Sandbox Code Playgroud)
回报
Index([u'b', u'c', u'f'], dtype='object')
Run Code Online (Sandbox Code Playgroud)
我想重命名"f"为"e".据pandas.MultiIndex.rename我说:
df.columns.rename(["b1", "c1", "f1"], level=1)
Run Code Online (Sandbox Code Playgroud)
但它提出了
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-110-b171a2b5706c> in <module>()
----> 1 df.columns.rename(["b1", "c1", "f1"], level=1)
C:\Users\USERNAME\AppData\Local\Continuum\Miniconda2\lib\site-packages\pandas\indexes\base.pyc in set_names(self, names, level, inplace)
994 if level is not None and …Run Code Online (Sandbox Code Playgroud)