【发布时间】:2021-04-16 04:09:02
【问题描述】:
我有一个看起来像这样的数据框
| Title | Ratings |
|---|---|
| Do schools kill creativity? | [{'id': 7, 'name': 'Funny', 'count': 19645}, {'id': 1, 'name': 'Beautiful', 'count': 4573}, {'id': 9, 'name': 'Ingenious', 'count': 6073}, {'id': 3, 'name': 'Courageous', 'count': 3253}, {'id': 11, 'name': 'Longwinded', 'count': 387}, {'id': 2, 'name': 'Confusing', 'count': 242}, {'id': 8, 'name': 'Informative', 'count': 7346}, {'id': 22, 'name': 'Fascinating', 'count': 10581}, {'id': 21, 'name': 'Unconvincing', 'count': 300}, {'id': 24, 'name': 'Persuasive', 'count': 10704}, {'id': 23, 'name': 'Jaw-dropping', 'count': 4439}, {'id': 25, 'name': 'OK', 'count': 1174}, {'id': 26, 'name': 'Obnoxious', 'count': 209}, {'id': 10, 'name': 'Inspiring', 'count': 24924}] |
| Simple designs to save a life | [{'id': 9, 'name': 'Ingenious', 'count': 269}, {'id': 3, 'name': 'Courageous', 'count': 92}, {'id': 7, 'name': 'Funny', 'count': 131}, {'id': 2, 'name': 'Confusing', 'count': 42}, {'id': 1, 'name': 'Beautiful', 'count': 91}, {'id': 8, 'name': 'Informative', 'count': 446}, {'id': 10, 'name': 'Inspiring', 'count': 397}, {'id': 22, 'name': 'Fascinating', 'count': 515}, {'id': 11, 'name': 'Longwinded', 'count': 45}, {'id': 21, 'name': 'Unconvincing', 'count': 49}, {'id': 24, 'name': 'Persuasive', 'count': 1234}, {'id': 25, 'name': 'OK', 'count': 73}, {'id': 23, 'name': 'Jaw-dropping', 'count': 139}, {'id': 26, 'name': 'Obnoxious', 'count': 21}] |
我想将 Ratings 中的数据解析为看起来像
| Title | Rating | Count |
|---|---|---|
| Do schools kill creativity? | Funny | 19645 |
| Do schools kill creativity? | Beautiful | 4573 |
我尝试使用 } 作为分隔符来分解数据
#explode ratings by title
df['ratings'] = df['ratings'].str.split('}')
df_explode_ratings = df.explode('ratings').reset_index(drop=True)
cols = list(df_explode_ratings.columns)
cols.append(cols.pop(cols.index('title')))
df_explode_ratings = df_explode_ratings[cols]
df_explode_cols = ['title', 'ratings']
df_explode_ratings = df_explode_ratings.drop(columns=[col for col in df_explode_ratings if col not in df_explode_cols])
这可行,但我仍然需要进一步解析它,我打算再次拆分 ,但在 Ratings 列中得到了 NaN 值。
【问题讨论】:
-
在你得到这个数据框之前会发生什么?看起来可以重新设计导致此数据结构的过程,以为您提供更有用的文件。如果没有,并且如果您没有大量的行,您甚至可以更好地循环行并使用
json模块将字符串加载到Ratings中。 -
嘿,谢谢,这是来自 Kaggle 的 .csv,看起来它是从 json 转储的,所以我无法控制文件中的数据集结构