【发布时间】:2018-07-16 20:34:59
【问题描述】:
目标
我有一个 Pandas 数据框,如下所示,我想加入 command 和 value 列,同时将其转换回原始字符串格式以写入 .cfg 文件。
数据框 - df:
之前加入:
command value
0 bind "0" "slot10"
1 bind "1" "slot1"
2 bind "2" "slot2"
3 bind "3" "slot3"
4 bind "4" "slot4"
5 bind "5" "slot5"
6 bind "6" "slot6"
7 bind "7" "slot7"
8 bind "8" "slot8"
9 bind "9" "slot9"
10 bind "a" "+moveleft"
11 bind "b" "buymenu"
12 bind "d" "+moveright"
之后加入:
0 bind "0" "slot10"
1 bind "1" "slot1"
2 bind "2" "slot2"
3 bind "3" "slot3"
4 bind "4" "slot4"
5 bind "5" "slot5"
6 bind "6" "slot6"
7 bind "7" "slot7"
8 bind "8" "slot8"
9 bind "9" "slot9"
10 bind "a" "+moveleft"
11 bind "b" "buymenu"
12 bind "d" "+moveright"
13 bind "e" "+use"
14 bind "f" "+lookatweapon"
...
etc.
dtype: object
我的尝试:
我已经设法使用以下代码将两列组合起来以获得上面的输出:
df = df['command'].astype(str)+' '+df['value'].astype(str)
但是,这仍然没有帮助将数据帧转换为原始字符串,以便可以将其写入 .cfg 文件。
我也尝试过使用df.to_string(),但这似乎没有什么不同
预期输出
我想获得像这样分配在要写入 .cfg 文件的变量下的原始字符串输出:
bind "0" "slot10"
bind "1" "slot1"
bind "2" "slot2"
bind "3" "slot3"
bind "4" "slot4"
bind "5" "slot5"
bind "6" "slot6"
bind "7" "slot7"
bind "8" "slot8"
bind "9" "slot9"
bind "a" "+moveleft"
bind "b" "buymenu"
bind "d" "+moveright"
bind "e" "+use"
bind "f" "+lookatweapon"
...
etc.
【问题讨论】:
标签: python python-2.7 pandas dataframe