在pandas中按列名称的子字符串熔化列(python) [英] melt column by substring of the columns name in pandas (python)
本文介绍了在pandas中按列名称的子字符串熔化列(python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有数据框:
subject A_target_word_gd A_target_word_fd B_target_word_gd B_target_word_fd subject_type
1 1 2 3 4 mild
2 11 12 13 14 moderate
我想将其融合为一个看起来如下的数据框: / p>
And I want to melt it to a dataframe that will look:
cond subject subject_type value_type value
A 1 mild gd 1
A 1 mild fg 2
B 1 mild gd 3
B 1 mild fg 4
A 2 moderate gd 11
A 2 moderate fg 12
B 2 moderate gd 13
B 2 moderate fg 14
...
...
含义,根据列名的定界符进行融合。
Meaning, to melt based on the delimiter of the columns name.
最好的方法是什么?
推荐答案
一个更多方法(非常类似于@ anky_91所发布的内容。在他发布之前已经开始键入它,因此将其放在那里。)
One more approach (very similar to what @anky_91 has posted. had already started typing it before he posted, hence putting it out there.)
new_df =pd.melt(df, id_vars=['subject_type','subject'], var_name='abc').sort_values(by=['subject', 'subject_type'])
new_df['cond']=new_df['abc'].apply(lambda x: (x.split('_'))[0])
new_df['value_type']=new_df.pop('abc').apply(lambda x: (x.split('_'))[-1])
new_df
输出
subject_type subject value cond value_type
0 mild 1 1 A gd
2 mild 1 2 A fd
4 mild 1 3 B gd
6 mild 1 4 B fd
1 moderate 2 11 A gd
3 moderate 2 12 A fd
5 moderate 2 13 B gd
7 moderate 2 14 B fd
这篇关于在pandas中按列名称的子字符串熔化列(python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文