简体   繁体   English

Python多个DataFrames到字典

[英]Python Multiple DataFrames to dictionary

I have several 2 column data frames with varying row counts. 我有几个具有不同行数的2列数据帧。 Essentially 1 column is the categorical string value and the second column is the label encoded numeric value. 本质上,第一列是分类字符串值,第二列是标签编码的数字值。 Now when I evaluate feature values from the model output, I only see the numeric value and I want to see the categorical value. 现在,当我从模型输出评估特征值时,我只看到数字值,而我想看到分类值。

Is the best way to do this to create a dictionary of lists/dictionaries and then loop through the dictionary where value equals and replace? 这样做的最好方法是创建列表/字典的字典,然后遍历值等于并替换的字典吗?

I'm open to a different approach. 我愿意接受另一种方法。

df1 = df1[['A1','A2']].drop_duplicates().sort_values(by=['A2'])
df2 = df2[['B1','B2']].drop_duplicates().sort_values(by=['B2'])
df3 = df3[['C1','C2']].drop_duplicates().sort_values(by=['C2'])

So... 所以...

df1
A1    A2
cat   1

df2
B1    B2
dog   2

df3
C1    C2
fish  3
bird  4

dict= {df1: {cat:1}, df2: {dog:2}, df3: {fish:3}} dict = {df1:{cat:1},df2:{dog:2},df3:{fish:3}}

Assuming your omission of 'bird' was an oversight: 假设您忽略了“鸟”是一个疏忽:

Code: 码:

import pandas as pd
df1 = pd.DataFrame(data = [['cat', 1]], columns=['A1', 'A2'])
df2 = pd.DataFrame(data=[['dog', 2]], columns=['B1', 'B2'])
df3 = pd.DataFrame(data=[['fish', 3], ['bird', 4]], columns=['C1', 'C2'])
result = {"df{}".format(i): dict(df.values.tolist()) for i, df in enumerate([df1, df2, df3], start=1)}
print(result)

Output: 输出:

{'df1': {'cat': 1}, 'df2': {'dog': 2}, 'df3': {'fish': 3, 'bird': 4}}

Alternatively, you can create a flat dictionary as follows. 或者,您可以按以下方式创建平面词典。 (DataFrame information is lost.) (DataFrame信息丢失。)

Alternative: 选择:

alt = {k: v for df in [df1, df2, df3] for k, v in df.values.tolist()}
print(alt)

Output: 输出:

{'cat': 1, 'dog': 2, 'fish': 3, 'bird': 4}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Python 从字典中提取多个数据帧 - Extract multiple dataframes from dictionary with Python 使用循环将字典中的多个数据帧保存到 Python 中的单独数据帧中 - Saving multiple dataframes within a dictionary into separate dataframes in Python using loop python groupby 在数据帧字典上 - python groupby on a dictionary of dataframes 连接数据帧字典 Python - Concatenating a dictionary of dataframes Python 如何将值从 Python Pandas 中的多个字典对象插入到 DataFrames 中 - How to insert values into DataFrames from multiple dictionary objects in Python Pandas 从数据框的python字典创建/获取/提取多个数据框 - Creating/Getting/Extracting multiple data frames from python dictionary of dataframes 在python for循环中设置变量/字典键以在pandas中加载多个数据帧 - Setting variables/dictionary keys in python for loop to load multiple dataframes in pandas 分别读取多个 CSV 并将它们保存到 Python 中并行的数据帧字典中 - Reading multiple CSVs separately and saving them into a dictionary of dataframes in parallel in Python 如何在 Python 中将字典项转换为多个数据帧? - How do I turn Dictionary items into multiple DataFrames in Python? 从 Python 数据帧字典中获取单独的数据帧 - Get separate dataframes from a dictionary of dataframes Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM