[英]How to split column into multiple columns in pandas with specific delimiter position?
here is my dataframe这是我的数据框
df_test
0 (-, 136), (-, 136), 1.0
1 (-, 136), (-, 438), 0.5
2 (-, 136), (-, 257), 0.5
I would like to see the result like this我想看到这样的结果
df_t1 df_t2 df_val
0 (-, 136) (-, 136) 1.0
1 (-, 136) (-, 438) 0.5
2 (-, 136) (-, 257) 0.5
I have used this code but it is not working我已经使用了此代码,但它不起作用
new_df[['df_t1', 'df_t2', 'df_val']] = new_df['df_test'].str.split(',',expand=True)
any suggestion?有什么建议吗?
Specific to your format, you can use ast.literal_eval
.具体到您的格式,您可以使用
ast.literal_eval
。 Better, try and solve the issue upstream before your dataframe is constructed.更好的是,在构建数据帧之前尝试解决上游的问题。
from ast import literal_eval
df = pd.DataFrame({'df_test': ['(-, 136), (-, 136), 1.0',
'(-, 136), (-, 438), 0.5',
'(-, 136), (-, 257), 0.5']})
series = df.pop('df_test').str.replace('-', '"-"').apply(literal_eval)
df = df.join(pd.DataFrame(series.values.tolist(), columns=['df_t1', 'df_t2', 'df_val']))
print(df)
df_t1 df_t2 df_val
0 (-, 136) (-, 136) 1.0
1 (-, 136) (-, 438) 0.5
2 (-, 136) (-, 257) 0.5
Use:用:
new_df[['df_t1', 'df_t2', 'df_val']] = new_df['df_test'].str.rsplit('),', expand=True)
new_df[['df_t1', 'df_t2']] += ')'
print (new_df)
df_test df_t1 df_t2 df_val
0 (-, 136), (-, 136), 1.0 (-, 136) (-, 136) 1.0
1 (-, 136), (-, 438), 0.5 (-, 136) (-, 438) 0.5
2 (-, 136), (-, 257), 0.5 (-, 136) (-, 257) 0.5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.