python - Pandas 放大多维物体

标签 python pandas multidimensional-array

我想将行添加到多维 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/

相关文章:

python - 在 matplotlib 中绘制时间增量

php - array_push 成一个多维数组

比较相同的单词

go - 需要将 2 维数组转换为字符串并将最后一个逗号替换为句号。(Golang)

python - 如何从 statsmodels 中 WLS 回归的 2D 参数获取测试预测

python - 需要帮助从文件中提取数据

python - 在 PyQt 中单击时悬停事件

python - 为什么此日志记录配置不打印到标准输出?

python - 唯一索引和单调索引有什么区别?

python - 使用 Pandas 计算累积平均值