您如何在 python 中同时将文本发送到 Columns 和 Unpivoting?也许使用 Pandas?
How do you do text to Columns & Unpivoting at the same time in python?? using Pandas maybe?
我想做这个:
进入这个:
我已经在 python 的数据框中有了第一个 table 作为 df3。如何使用 pandas 最好在 python 中获得我想要的输出?
假设输入:
ID Invoice DO other_cols
0 A a 1,2,3 xxx
1 B b 4,5 xxx
2 C c 6 xxx
您可以使用 assign
+str.split
将您的字符串转换为列表,并使用 explode
为列表中的每个项目生成一行:
(df.assign(DO=df['DO'].str.split(','))
.explode('DO')
)
输出:
ID Invoice DO other_cols
0 A a 1 xxx
0 A a 2 xxx
0 A a 3 xxx
1 B b 4 xxx
1 B b 5 xxx
2 C c 6 xxx
我想做这个:
进入这个:
我已经在 python 的数据框中有了第一个 table 作为 df3。如何使用 pandas 最好在 python 中获得我想要的输出?
假设输入:
ID Invoice DO other_cols
0 A a 1,2,3 xxx
1 B b 4,5 xxx
2 C c 6 xxx
您可以使用 assign
+str.split
将您的字符串转换为列表,并使用 explode
为列表中的每个项目生成一行:
(df.assign(DO=df['DO'].str.split(','))
.explode('DO')
)
输出:
ID Invoice DO other_cols
0 A a 1 xxx
0 A a 2 xxx
0 A a 3 xxx
1 B b 4 xxx
1 B b 5 xxx
2 C c 6 xxx