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) 我有DataFrame像这样的Pandas(版本0.14.1)对象
import pandas as pd
df = pd.DataFrame(zip([1, 2, 3, 4, 5],
[0.1, 0.3, 0.1, 0.2, 0.4]),
columns=['y', 'dy'])
Run Code Online (Sandbox Code Playgroud)
它回来了
y dy
0 1 0.1
1 2 0.3
2 3 0.1
3 4 0.2
4 5 0.4
Run Code Online (Sandbox Code Playgroud)
其中第一列是值,第二列是错误.
第一种情况:我想为y-values 制作一个情节
df['y'].plot(style="ro-")
Run Code Online (Sandbox Code Playgroud)
第一个案例http://i59.tinypic.com/aensc0.jpg
情况二:我想补充一个垂直errorbars dy为y-值
df['y'].plot(style="ro-", yerr=df['dy'])
Run Code Online (Sandbox Code Playgroud)
第二个案例http://i58.tinypic.com/huiloj.png
所以,如果我向方法添加yerr或xerr参数plot,它会忽略style.
它是熊猫特色还是虫子?
我创建了这个数据框:
import pandas as pd
columns = pd.MultiIndex.from_tuples([("x", "", ""), ("values", "a", "a.b"), ("values", "c", "")])
df0 = pd.DataFrame([(0,10,20),(1,100,200)], columns=columns)
df0
Run Code Online (Sandbox Code Playgroud)
我卸载df0到excel:
df0.to_excel("test.xlsx")
Run Code Online (Sandbox Code Playgroud)
并再次加载它:
df1 = pd.read_excel("test.xlsx", header=[0,1,2])
df1
Run Code Online (Sandbox Code Playgroud)
我有Unnamed :...列名。
为了df1看起来像初始df0我运行:
def rename_unnamed(df, label=""):
for i, columns in enumerate(df.columns.levels):
columns = columns.tolist()
for j, row in enumerate(columns):
if "Unnamed: " in row:
columns[j] = ""
df.columns.set_levels(columns, level=i, inplace=True)
return df
rename_unnamed(df1)
Run Code Online (Sandbox Code Playgroud)
做得好。但是有没有大熊猫的方式来做到这一点?
我想用熊猫绘制不对称的误差线。根据官方文档,这应该有效
df = pd.DataFrame([[1,0.2,0.7]])
fig, ax = plt.subplots()
df[0].plot.bar(yerr=[df[1], df[2]], ax=ax)
Run Code Online (Sandbox Code Playgroud)
但是pandas 渲染误差条作为df[1]上下限(-0.2/+0.2 而不是-0.2/+0.7):
我在哪里犯了错误?
我在 Windows 7 下使用 pandas v0.20.3 和 python v2.7.13。