使用unstack()无效
import pandas as pd
data = [
{"content": "1", "title": "刘德华", "info": "", "time": 1578877014},
{"content": "2", "title": "刘德华", "info": "", "time": 1579877014},
{"content": "3", "title": "刘德华", "info": "", "time": 1582877014},
{"content": "12", "title": "苹果", "info": "", "time": 1582876014},
{"content": "33", "title": "apple", "info": "", "time": 1581877014},
{"content": "16", "title": "banana", "info": "", "time": 1561877014},
]
so = pd.DataFrame(data)
# 最大3个值
hao=pd.to_numeric(so['content']).nlargest(3,keep='all')
hao.unstack()
原因就是 DataFrame 数据类型有 index和 columns这两个属性,而上面的 Series 中只有一个索引,所以会报错。我们重置索引即可:
import pandas as pd
data = [
{"content": "1", "title": "刘德华", "info": "", "time": 1578877014},
{"content": "2", "title": "刘德华", "info": "", "time": 1579877014},
{"content": "3", "title": "刘德华", "info": "", "time": 1582877014},
{"content": "12", "title": "苹果", "info": "", "time": 1582876014},
{"content": "33", "title": "apple", "info": "", "time": 1581877014},
{"content": "16", "title": "banana", "info": "", "time": 1561877014},
]
so = pd.DataFrame(data)
# 最大3个值
hao=pd.to_numeric(so['content']).nlargest(3,keep='all')
hao.reset_index()