[英]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.