删除numpy数组的熊猫python导出到excel中的方括号

时间:2018-08-07 11:32:15

标签: python pandas

使用以下功能:

def exportExcel(data):
    df = pd.DataFrame(data)
    filepath = 'Output.xlsx'
    df.to_excel(filepath, index=False) 

将np.array导出到excel列后的输出

    column a       column b     column c
    [[0.00536237] [0.00536237]] [0.01030928]
    [[0.00652899] [0.00652899]] [0.]
    [[0.00579218] [0.00579218]] [0.]

如何删除所有方括号?

我尝试了几种解决方案,但是它们不起作用:

for col in df:
df[col] = df[col].str.extract(r'\[(.*)\]')

谢谢

2 个答案:

答案 0 :(得分:2)

如果它们是string,则可以使用replace

df[col] = df[col].str.replace('[','')
df[col] = df[col].str.replace(']','')

或者,如果要删除的方括号仅位于字符串的开头或结尾,请使用strip

df[col] = df[col].str.strip('[')
df[col] = df[col].str.strip(']')

答案 1 :(得分:2)

如果该列为numpy,则可以使用函数np.squeeze()

df[col]=np.squeeze(df[col].tolist())