python - Split a column of a dataframe into two separate columns
问题描述
I'd like to split a column of a dataframe into two separate columns. Here is how my dataframe looks like (only the first 3 rows):
I'd like to split the column referenced_tweets
into two columns: type
and id
in a way that for example, for the first row, the value of the type
column would be replied_to
and the value of id
would be 1253050942716551168
.
Here is what I've tried:
df[['type', 'id']] = df['referenced_tweets'].str.split(',', n=1, expand=True)
but I get the error:
ValueError: Columns must be the same length as key
(I think I get this error because the type in the referenced_tweets column is NOT always replied_to
(e.g., it can be retweeted
, and therefore, the lengths would be different)
解决方案
Why not get the values from the dict and add it two new columns?
def unpack_column(df_series, key):
""" Function that unpacks the key value of your column and skips NaN values """
return [None if pd.isna(value) else value[0][key] for value in df_series]
df['type'] = unpack_column(df['referenced_tweets'], 'type')
df['id'] = unpack_column(df['referenced_tweets'], 'id')
or in a one-liner:
df[['type', 'id']] = df['referenced_tweets'].apply(lambda x: (x[0]['type'], x[0]['id']))
推荐阅读
- python - 在 Python 中从大型数据集绘图
- c# - 夏普序列化器使用自定义对象构造函数?
- google-app-engine - 将 Jira REST API 部署到 Googe Bigquery
- javascript - 直接 DOM 操作或访问
- java - Spring,Apache Avro,Allargs 构造函数
- java - 使用 Jedis 的 Redis 中单个键占用的内存?
- android - 离子 - 实时部署不适用于 Android <5.0
- javascript - 如何在没有刷新页面的情况下清空文之信引导表中点击事件的搜索框?
- angular - 设置布尔打字稿的默认值
- swift - 斯威夫特:在出现后杀死接收者