当一行以字符串开头时将列拆分为多列
Split column into multiple columns when a row starts with a string
只要列中的一行以特定字符串开头,我想将数据框中的单个列拆分为多个列。我有一个单列的数据框,如下所示:
import pandas as pd
lst = ['Group 123 nv-1', 'a, v', 's,b', 'y, i', 'Group 123 mt-d2', 'b, v',
'Group 123 id-01', 'n,m', 'x, y', 'z, m', 'l,b']
df = pd.DataFrame(lst, columns=['Group'])
print(df)
Group
0 Group 123 nv-1
1 a, v
2 s,b
3 y, i
4 Group 123 mt-d2
5 b, v
6 Group 123 id-01
7 n,m
8 x, y
9 z, m
10 l,b
每当字符串以“Group 123”开头时,我想将 Group 列拆分为新列:
Group 123 nv-1 Group 123 mt-d2 Group 123 id-01
a, v b, v n,m
s,b x, y
y, i z, m
l,b
试试这个:
pd.concat([sub.reset_index(drop=True) for _, sub in df.groupby(
df.Group.str.contains(r'^Group\s+123').cumsum())], axis=1)
>>>
Group Group Group
0 Group 123 nv-1 Group 123 mt-d2 Group 123 id-01
1 a, v b, v n,m
2 s,b NaN x, y
3 y, i NaN z, m
4 NaN NaN l,b
只要列中的一行以特定字符串开头,我想将数据框中的单个列拆分为多个列。我有一个单列的数据框,如下所示:
import pandas as pd
lst = ['Group 123 nv-1', 'a, v', 's,b', 'y, i', 'Group 123 mt-d2', 'b, v',
'Group 123 id-01', 'n,m', 'x, y', 'z, m', 'l,b']
df = pd.DataFrame(lst, columns=['Group'])
print(df)
Group
0 Group 123 nv-1
1 a, v
2 s,b
3 y, i
4 Group 123 mt-d2
5 b, v
6 Group 123 id-01
7 n,m
8 x, y
9 z, m
10 l,b
每当字符串以“Group 123”开头时,我想将 Group 列拆分为新列:
Group 123 nv-1 Group 123 mt-d2 Group 123 id-01
a, v b, v n,m
s,b x, y
y, i z, m
l,b
试试这个:
pd.concat([sub.reset_index(drop=True) for _, sub in df.groupby(
df.Group.str.contains(r'^Group\s+123').cumsum())], axis=1)
>>>
Group Group Group
0 Group 123 nv-1 Group 123 mt-d2 Group 123 id-01
1 a, v b, v n,m
2 s,b NaN x, y
3 y, i NaN z, m
4 NaN NaN l,b