我有如下数据: 字段值
0 CRD 146099
1 LegalName CHUNG, BUCK CHWEE
2 BusName PRINCIPA FINANCIAL ADVISORS
3 URL https://adviserinfo.sec.gov/IAPD/content/ViewF...
4 CRD 170701
5 LegalName MESSINA AND ASSOCIATES, INC
6 BusName FINANCIAL RESOURCES GROUP
7 URL https://adviserinfo.sec.gov/IAPD/content/ViewF...
8 CRD 133630
9 LegalName ALAN EDELMAN
10 BusName EDELMAN, ALAN
11 URL https://adviserinfo.sec.gov/IAPD/content/ViewF...
12 CRD 131792
13 LegalName RESOURCE MANAGEMENT LLC
14 BusName RESOURCE MANAGEMENT LLC
15 URL https://adviserinfo.sec.gov/IAPD/content/ViewF...
如何将其转换为CRD,LegalName,BusName,URL为列。我尝试使用pd.melt,但这似乎不是我想要的。
答案 0 :(得分:1)
首先将split
用于2列,然后通过cumcount
创建计数器Series
,通过set_index
创建MultiIndex
并通过unstack
重塑形状:
df[['Field','Value']] = df['Value'].str.split(n=1, expand=True)
groups = df.groupby('Field').cumcount()
df = df.set_index([groups, 'Field'])['Value'].unstack()
print (df)
Field BusName CRD LegalName \
0 PRINCIPA FINANCIAL ADVISORS 146099 CHUNG, BUCK CHWEE
1 FINANCIAL RESOURCES GROUP 170701 MESSINA AND ASSOCIATES, INC
2 EDELMAN, ALAN 133630 ALAN EDELMAN
3 RESOURCE MANAGEMENT LLC 131792 RESOURCE MANAGEMENT LLC
Field URL
0 https://adviserinfo.sec.gov/IAPD/content/ViewF...
1 https://adviserinfo.sec.gov/IAPD/content/ViewF...
2 https://adviserinfo.sec.gov/IAPD/content/ViewF...
3 https://adviserinfo.sec.gov/IAPD/content/ViewF...
答案 1 :(得分:0)
我认为您正在寻找DataFrame。transpose