我目前將我的訓練示例存盤在 python 串列中。每個訓練示例都是具有以下結構的字典:
example = {
"features" : {
"position" : np.array([[0,1], [1,2], [2, 3], [3, 4]]),
"type" : np.array([-1, -1, 2, 1])
}
"labels" : np.array([[2,1], [3,2], [4, 3], [5, 4]])
}
將其存盤在熊貓資料框中的正確結構是什么?我可以將 numpy 陣列作為列資料型別嗎?我認為那將是理想的。但這似乎不可能?
uj5u.com熱心網友回復:
也許可以嘗試使用pd.json_normalize,也可以df.explode根據您希望如何處理陣列中的每一行來選擇:
import pandas as pd
import tabulate
example = {
"features" : {
"position" : np.array([[0,1], [1,2], [2, 3], [3, 4]]),
"type" : np.array([-1, -1, 2, 1])
},
"labels" : np.array([[2,1], [3,2], [4, 3], [5, 4]])
}
df = pd.json_normalize(example, sep='_')
# df = pd.json_normalize([example, example], sep='_') <-- for a list of examples
print(df.to_markdown())
| | labels | features_position | features_type |
|---:|:---------|:--------------------|:----------------|
| 0 | [[2 1] | [[0 1] | [-1 -1 2 1] |
| | [3 2] | [1 2] | |
| | [4 3] | [2 3] | |
| | [5 4]] | [3 4]] | |
print(df.explode('features_type').to_markdown())
| | labels | features_position | features_type |
|---:|:---------|:--------------------|----------------:|
| 0 | [[2 1] | [[0 1] | -1 |
| | [3 2] | [1 2] | |
| | [4 3] | [2 3] | |
| | [5 4]] | [3 4]] | |
| 0 | [[2 1] | [[0 1] | -1 |
| | [3 2] | [1 2] | |
| | [4 3] | [2 3] | |
| | [5 4]] | [3 4]] | |
| 0 | [[2 1] | [[0 1] | 2 |
| | [3 2] | [1 2] | |
| | [4 3] | [2 3] | |
| | [5 4]] | [3 4]] | |
| 0 | [[2 1] | [[0 1] | 1 |
| | [3 2] | [1 2] | |
| | [4 3] | [2 3] | |
| | [5 4]] | [3 4]] | |
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/415734.html
標籤:
上一篇:對資料框串列進行排序
下一篇:根據多列值從資料框中選擇值
