社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

使用python 3[duplicate]将带有列表的字典转换为数据帧

Zephyr • 3 年前 • 1729 次点击  

我正在尝试将列表中的字典转换为以键为列名的数据帧。以下是样本数据。

df = [{'id': '755', 'player_name': 'Jamie Vardy', 'games': '35', 'time': '3034', 'goals': '23', 'xG': '18.903537318110466', 'assists': '5', 'xA': '6.3682975601404905', 'shots': '89', 'key_passes': '32', 'yellow_cards': '3', 'red_cards': '0', 'position': 'F S', 'team_title': 'Leicester', 'npg': '19', 'npxG': '15.097693115472794', 'xGChain': '21.02660731226206', 'xGBuildup': '1.7243406660854816'}, {'id': '318', 'player_name': 'Pierre-Emerick Aubameyang', 'games': '36', 'time': '3143', 'goals': '22', 'xG': '16.352623080834746', 'assists': '3', 'xA': '4.492486916482449', 'shots': '93', 'key_passes': '26', 'yellow_cards': '3', 'red_cards': '1', 'position': 'F M S', 'team_title': 'Arsenal', 'npg': '20', 'npxG': '14.830358987674117', 'xGChain': '19.964282035827637', 'xGBuildup': '5.339657470583916'}]

我可以这样称呼每一本字典 df[0] 还有钥匙 df[0].keys() .

我使用以下代码转换为dataframe。

cols = list (df[0].keys())
df_new = pd.DataFrame.from_dict(df[0], orient='index',
                  columns = cols )

这让我犯了一个错误: ValueError: Shape of passed values is (18, 1), indices imply (18, 18) 有人能给我建议吗?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/130616
 
1729 次点击  
文章 [ 1 ]  |  最新文章 3 年前