我想将行添加到多维 pd.Series。
b = pd.Series([1,1,2,3], index = [["digit", "digit", "digit", "digit"], ["one", "one", "two", "three"]])
b
Out[30]:
digit one 1
one 1
two 2
three 3
dtype: int64
现在,当我这样做
b.loc["digit"].loc["four"] = 4
什么也没发生。没有错误,但 b 保持不变。 虽然这种方法适用于单维系列
最佳答案
您需要sort_index
因为:
PerformanceWarning: indexing past lexsort depth may impact performance.
然后元组为 loc
:
b = b.sort_index()
b.loc[("digit", "four")] = 4
print (b)
digit one 1
one 1
three 3
two 2
four 4
dtype: int64
另一个解决方案 concat
:
c = pd.Series([4], index=pd.MultiIndex.from_arrays([['digit'],['four']]))
b = pd.concat([b, c])
print (b)
digit one 1
one 1
two 2
three 3
four 4
dtype: int64
关于python - Pandas 放大多维物体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44996599/