我有一个数据框,需要从中删除方括号才能输入到循环中。
我尝试使用str.strip和str.commands。但是我收到错误消息,说我必须传递一个带有布尔值的数据框。看来我数据框中的值是列表。
m2 = pd.read_csv('newm2.csv', sep=',s', header=None)
print m2
样本输入:
m2 = pd.DataFrame([
[[14,38,51,65,84,85]],
[[3,34,58,65,66,75]],
[[3,15,68,70,80,82]],
[[19,31,42,50,54,97]],
[[4,9,48,62,74,77]],
])
0
0 [14,38,51,65,84,85]
1 [3,34,58,65,66,75]
2 [3,15,68,70,80,82]
3 [19,31,42,50,54,97]
4 [4,9,48,62,74,77]
上面是当前正在打印内容的一个小示例。我需要每一行看起来像:
"14,38,51,65,84,85"
我该如何解决问题?
答案 0 :(得分:0)
一种简单的方法是将list
转换为str
:
x = [
[[14,38,51,65,84,85]],
[[3,34,58,65,66,75]],
[[3,15,68,70,80,82]],
[[19,31,42,50,54,97]],
[[4,9,48,62,74,77]],
]
m2 = pd.DataFrame(x)
m2[0] = m2[0].apply(lambda x: ','.join([str(i) for i in x]))
m2
Out[1]:
0
0 '14,38,51,65,84,85'
1 '3,34,58,65,66,75'
2 '3,15,68,70,80,82'
3 '19,31,42,50,54,97'
4 '4,9,48,62,74,77'
修改
如果行是str
类型,而不是list
类型,该怎么办。我们只是将它们转换为可理解为原义的str,然后执行.join
from ast import literal_eval
x = [
['[14,38,51,65,84,85]'],
['[3,34,58,65,66,75]'],
['[3,15,68,70,80,82]'],
['[19,31,42,50,54,97]'],
['[4,9,48,62,74,77]'],
]
m2 = pd.DataFrame(x)
m2[0] = m2[0].apply(lambda x: ','.join([str(i) for i in literal_eval(x)]))
m2
Out[1]:
0
0 '14,38,51,65,84,85'
1 '3,34,58,65,66,75'
2 '3,15,68,70,80,82'
3 '19,31,42,50,54,97'
4 '4,9,48,62,74,77'
答案 1 :(得分:0)
由于性能较差,我会避免使用apply
。这是另一种方式。
m2 = pd.DataFrame([
[[14,38,51,65,84,85]],
[[3,34,58,65,66,75]],
[[3,15,68,70,80,82]],
[[19,31,42,50,54,97]],
[[4,9,48,62,74,77]],
])
m2.iloc[:, 0] = m2.iloc[:, 0].astype(str)
m2.iloc[:, 0] = ['"' + x.strip('[').strip(']').replace(' ','') + '"' for x in m2.iloc[:, 0]]
m2
输出:
0
0 "14,38,51,65,84,85"
1 "3,34,58,65,66,75"
2 "3,15,68,70,80,82"
3 "19,31,42,50,54,97"
4 "4,9,48,62,74,77"
答案 2 :(得分:0)
为
a = [1,2,3,4]
可以
b = str(a) # out: '[1,2,3,4]'
避免使用'['和']'
b = str(a)[1:-1] # out: '1,2,3,4'
所以,我们只需要将其应用于数组m2 [0]
中的每个数组import pandas as pd
m2 = pd.DataFrame([
[[14,38,51,65,84,85]],
[[3,34,58,65,66,75]],
[[3,15,68,70,80,82]],
[[19,31,42,50,54,97]],
[[4,9,48,62,74,77]],
])
m2[0] = m2[0].apply(lambda x: str(x)[1:-1])
print(m2[0])
输出:
0 14, 38, 51, 65, 84, 85
1 3, 34, 58, 65, 66, 75
2 3, 15, 68, 70, 80, 82
3 19, 31, 42, 50, 54, 97
4 4, 9, 48, 62, 74, 77