[英]Pandas: Restructure dataframe from column values
The pandas dataframe includes two columns 'A' and 'B' 大熊猫数据框包括两列“ A”和“ B”
A B
1 a b
2 a c d
3 x
Each value in column 'B' is a string containing a variable number of letters separated by spaces. “ B”列中的每个值都是一个字符串,其中包含可变数量的字母,并用空格分隔。
Is there a simple way to construct: 有没有一种简单的方法来构造:
A B
1 a
1 b
2 a
2 c
2 d
3 x
You can use the following: 您可以使用以下内容:
splitted = df.set_index("A")["B"].str.split(expand=True)
stacked = splitted.stack().reset_index(1, drop=True)
result = stacked.to_frame("B").reset_index()
print(result)
A B
0 1 a
1 1 b
2 2 a
3 2 c
4 2 d
5 3 x
For the sub steps, see below: 有关子步骤,请参见下文:
print(splitted)
0 1 2
A
1 a b None
2 a c d
3 x None None
print(stacked)
A
1 a
1 b
2 a
2 c
2 d
3 x
dtype: object
Or you may also use pd.melt
: 或者您也可以使用
pd.melt
:
splitted = df["B"].str.split(expand=True)
pd.melt(splitted.assign(A=df.A), id_vars="A", value_name="B")\
.dropna()\
.drop("variable", axis=1)\
.sort_values("A")
A B
0 1 a
3 1 b
1 2 a
4 2 c
7 2 d
2 3 x
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.