分割DF的字符串列以添加额外的列



我有一个只有1列的数据框架,格式为

date, hh:mm - username/phonenumber: message

的例子:

08/04/2021, 17:19 - Rajat: Let's go

我想把它转换成有列的数据框架:

date, hh, mm, username/phno, message

Thanks, in advance

你可以尝试多次使用split,直到你得到你想要的效果,就像这样:

df = pd.DataFrame({'col1': "08/04/2021, 17:19 - Rajat: Let's go"}, index=[0])
df[['col2', 'col3']] = df['col1'].str.split("-", expand=True)
df[['date', 'hh:mm']] = df['col2'].str.split(', ', expand=True)
df[['username/phno', 'message']] = df['col3'].str.split(': ', expand=True)
df[['hh', 'mm']] = df['hh:mm'].str.split(':', expand=True)
# Delete leftover columns
df.drop(['col1', 'col2', 'col3', 'hh:mm'], axis=1, inplace=True)
# Change the order of the columns
df = df.loc[:, ["date", "hh", "mm", "username/phno", "message"]]

最新更新