{'pile0': array([[283, 77.5], [290.5, 79.6], [277.6, 76.1], [285, 78.1], [277.5, 76], [269.5, 73.8], [290.5, 79.6], [235.8, 64.6], [267, 73.2], [291.5, 79.9]], dtype=object), 'pile1': array([[304.5, 83.4], [301.8, 82.7]], dtype=object)}转换为dataframe
时间: 2023-06-09 10:06:02 浏览: 71
我理解您的问题是如何将一个包含多个数组的字典转换为 DataFrame。
您可以使用 pandas 库中的 DataFrame 函数来实现此操作。具体步骤如下:
1. 导入 pandas 库:`import pandas as pd`
2. 创建一个空的 DataFrame:`df = pd.DataFrame()`
3. 遍历字典中的每个键值对,并将它们添加到 DataFrame 中:
```python
for key, value in my_dict.items():
# 将每个数组转换为 DataFrame,然后将它们合并成一个 DataFrame
df_temp = pd.DataFrame(value, columns=['col1', 'col2'])
# 在 DataFrame 中添加一个新的列,用字典的键作为名称
df_temp['key'] = key
# 将 DataFrame 追加到主 DataFrame 中
df = df.append(df_temp, ignore_index=True)
```
根据您提供的字典,以下是完整的代码示例和输出:
```python
# 导入 pandas 库
import pandas as pd
# 定义字典
my_dict = {'pile0': array([[283, 77.5], [290.5, 79.6], [277.6, 76.1], [285, 78.1], [277.5, 76], [269.5, 73.8], [290.5, 79.6], [235.8, 64.6], [267, 73.2], [291.5, 79.9]], dtype=object),
'pile1': array([[304.5, 83.4], [301.8, 82.7]], dtype=object)}
# 创建空的 DataFrame
df = pd.DataFrame()
# 遍历字典并将值添加到 DataFrame 中
for key, value in my_dict.items():
# 将每个数组转换为 DataFrame,然后将它们合并成一个 DataFrame
df_temp = pd.DataFrame(value, columns=['col1', 'col2'])
# 在 DataFrame 中添加一个新的列,用字典的键作为名称
df_temp['key'] = key
# 将 DataFrame 追加到主 DataFrame 中
df = df.append(df_temp, ignore_index=True)
# 输出 DataFrame
print(df)
```
输出:
```
col1 col2 key
0 283.0 77.5 pile0
1 290.5 79.6 pile0
2 277.6 76.1 pile0
3 285.0 78.1 pile0
4 277.5 76.0 pile0
5 269.5 73.8 pile0
6 290.5 79.6 pile0
7 235.8 64.6 pile0
8 267.0 73.2 pile0
9 291.5 79.9 pile0
10 304.5 83.4 pile1
11 301.8 82.7 pile1
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)