简体   繁体   English

如何在将 dict 转换为 pandas DataFrame 时设置列标题(其中列名与 dict 键不匹配)?

[英]How to set column headers while converting dict to pandas DataFrame (where column names do not match dict keys)?

Using mongodb, I am outputting the unique field values and their number.使用 mongodb,我正在输出唯一的字段值及其编号。 Now I need to put this list in the ramp for further convenient work.现在我需要把这个清单放在坡道上,以方便工作。 But I don't quite understand how to do it because of the structure of the list.但是由于列表的结构,我不太明白该怎么做。

unique_data = db.small_311.aggregate([ 
{'$group' : {'_id' : "$Complaint Type", 'uniqueCount' : {'$sum': 1}}}])

Part of the list部分名单

print(list(unique_data))
[{'_id': 'Posting Advertisement', 'uniqueCount': 5}, {'_id': 'APPLIANCE', 'uniqueCount': 445}, {'_id': 'Complaint', 'uniqueCount': 9},...]

I am trying to add to the dataframe我正在尝试添加到 dataframe

data_result = list(unique_data)
df = pd.DataFrame(data_result, columns =['Complaint Type', 'Value'])
df[:5]

But the result is an empty dataframe (it contains only the name of the fields, no data) Result dataframe但是结果是一个空的 dataframe(它只包含字段的名称,没有数据)结果 dataframe

If you're just trying to rename your columns, you can use set_axis :如果您只是想重命名列,可以使用set_axis

pd.DataFrame(d).set_axis(['A', 'B'], axis=1)

                       A    B
0  Posting Advertisement    5
1              APPLIANCE  445
2              Complaint    9

or just assign df.columns to a list directly.或者直接将df.columns分配给列表。


To explain the output you're seeing, notice the columns argument does not match the keys in the records being passed to pd.DataFrame , so pandas doesn't select any columns. To explain the output you're seeing, notice the columns argument does not match the keys in the records being passed to pd.DataFrame , so pandas doesn't select any columns. It doesn't understand you're trying to rename the columns.它不明白您正在尝试重命名列。

Just passing your data as-is without any keyword arguments works:只需按原样传递数据,无需任何关键字 arguments 即可:

pd.DataFrame(d)

                     _id  uniqueCount
0  Posting Advertisement            5
1              APPLIANCE          445
2              Complaint            9

If you want just the _id column, that's when you would use columns param.如果您只想要 _id 列,那么您将使用columns参数。

pd.DataFrame(d, columns=['_id'])

                     _id
0  Posting Advertisement
1              APPLIANCE
2              Complaint

Passing a non-existent column will make it silently spit out nothing:传递一个不存在的列将使它无声无息地吐出:

pd.DataFrame(d, columns=['blah'])

   blah
0   NaN
1   NaN
2   NaN

暂无
暂无

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

相关问题 如何将字典键与数据框列条目匹配以插入值 - How to match dict keys with dataframe column entries to insert the values 如何在将dict键设为第一列而不是索引的同时,从字典转到pandas DataFrame再到CSV - How to go from dictionary to pandas DataFrame to CSV while making the dict keys the first column, not the index 从嵌套dict创建pandas数据帧,外键为df索引和内键列标题 - Create pandas dataframe from nested dict with outer keys as df index and inner keys column headers 熊猫和字典:将Dict转换为DataFrame并将值中的内部键用作DataFrame列标题 - Pandas and Dictionary: Convert Dict to DataFrame and use inner keys in values as DataFrame column headers 将字典值增量添加到pandas DataFrame。 具有dict键的列名称的DataFrame - Incremental addition of dictionary values to a pandas DataFrame. DataFrame with column names of dict keys 从dict到dict在Pandas Dataframe中的列顺序 - Column Order in Pandas Dataframe from dict of dict 如何通过dict列过滤熊猫数据框? - How to filter a pandas dataframe by dict column? 使用字典中的键在 pandas dataframe 中查找相同的“键”,然后将字典中的值分配给 dataframe 空列 - Using keys from a dict to look for the same "keys" in a pandas dataframe, then assign value from dict to dataframe empty column 清理 pandas dataframe 中的 dict 列 - Clean dict column in pandas dataframe 熊猫数据框按列进行分组 - Pandas Dataframe to dict grouping by column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM