小编Wei*_*hen的帖子

如何将没有双引号元素的列表的字符串表示形式转换为实际列表?

我参考了许多来源,但大多数用途json.loads()ast.literal_eval()不适用于我的情况:

x = '[(0.0, 58.099669), 56565, Raining]'
Run Code Online (Sandbox Code Playgroud)

预期输出:

x = [(0.0, 58.099669), 56565, 'Raining']
Run Code Online (Sandbox Code Playgroud)

有什么解决方法吗?

python string

5
推荐指数
1
解决办法
85
查看次数

使用另一列的值创建虚拟变量

我知道熊猫有一个 get_dummies() 函数。我想要做的不仅仅是为其添加 1/0 值,而是使用另一列中的值。

我有以下示例:

Id A B
1 a 1
2 a 2
3 b 3
4 b 4
5 b 5
6 c 6
Run Code Online (Sandbox Code Playgroud)

我想把它变成:

ID A_a A_b A_c
1 1 0 0
2 2 0 0
3 0 3 0
4 0 4 0
5 0 5 0
6 0 0 6
Run Code Online (Sandbox Code Playgroud)

其中哑变量的值来自 B 列。 ID 是识别的缩写。

python pandas

2
推荐指数
1
解决办法
257
查看次数

标签 统计

python ×2

pandas ×1

string ×1