Pandas 在python中基于字符串值创建单独的列
我是个新手,但我使用的数据框架如下所示:Pandas 在python中基于字符串值创建单独的列,pandas,dataframe,Pandas,Dataframe,我是个新手,但我使用的数据框架如下所示: ID Tag 123 Asset Class > Equity 123 Inquiry Type > Demonstration 123 Inquiry Topic > Holdings 456 Asset Class > Fixed Income 456 Inquiry Type > BF 456 Inquiry Topic &g
ID Tag
123 Asset Class > Equity
123 Inquiry Type > Demonstration
123 Inquiry Topic > Holdings
456 Asset Class > Fixed Income
456 Inquiry Type > BF
456 Inquiry Topic > VaR
ID Asset Type Inquiry Type Inquiry Topic
123 Equity Demonstration Holdings
456 Fixed Income Bf VaR
我想做的是将其更改为如下所示的数据帧:
ID Tag
123 Asset Class > Equity
123 Inquiry Type > Demonstration
123 Inquiry Topic > Holdings
456 Asset Class > Fixed Income
456 Inquiry Type > BF
456 Inquiry Topic > VaR
ID Asset Type Inquiry Type Inquiry Topic
123 Equity Demonstration Holdings
456 Fixed Income Bf VaR
对不起,如果这很简单;然而,我对这种操纵有一个问题。我试过了,但这似乎并没有完成我想要完成的 使用并通过索引选择拆分列表:
s = df['Tag'].str.split(' > ')
df = (pd.pivot(index=df['ID'], columns=s.str[0], values=s.str[1])
.reset_index()
.rename_axis(None, 1))
print (df)
ID Asset Class Inquiry Topic Inquiry Type
0 123 Equity Holdings Demonstration
1 456 Fixed Income VaR BF
非常感谢你的帮助@加隆史密斯-不客气!如果我的回答有帮助,别忘了。谢谢