简体   繁体   English

to_CSV将np.array保存为字符串而不是列表

[英]to_CSV saves np.array as string instead of as a list

I want to save a pandas dataframe as a csv file, the problem is that to_csv is converting the np.array into a string. 我想将熊猫数据框另存为csv文件,问题是to_csv将np.array转换为字符串。

I want to save the array as an array, I could not find anything in the documentation that was useful. 我想将数组另存为数组,但在文档中找不到任何有用的东西。

sudoku_solution = [a for a in assignment if a > 0]


label = np.reshape(np.array(sudoku_solution*n_splits), 
                   (n_splits, len(sudoku_solution)))

df = pd.DataFrame(zip(label))

path = './data/SplitsLabel.csv'
try:
    df.to_csv(path_or_buf = path, 
              mode = 'a',
              header = False)

solution_sudoku = [123, 345, 894, 324, 321, 321] (list of integers) solution_sudoku = [123,345,894,324,321,321](整数列表)

n_splits = 3 (integer) n_splits = 3(整数)

The final results should be something like: 最终结果应类似于:

0,[123 345 894 324 321 321] 0,[123345894894321321321]

1,[123 345 894 324 321 321] 1,[123345894324321321]

3,[123 345 894 324 321 321] 3,[123345894894321321321]

But the result now is: 但是现在的结果是:

0,"[123 345 894 324 321 321]" 0,“ [[123 345 894 324 321 321]”

1,"[123 345 894 324 321 321]" 1,“ [123 345 894 324 321 321]”

3,"[123 345 894 324 321 321]" 3,“ [123 345 894 324 321 321]”

How do I get rid of those quotes? 我如何摆脱那些报价?

I suspect that since your output includes commas that it may be entering quotes to avoid a conflict with the formatting. 我怀疑由于您的输出中包含逗号,因此可能会在输入引号中避免与格式冲突。 You could try changing your delimiter to a tab so this conflict doesnt happen. 您可以尝试将定界符更改为选项卡,这样就不会发生这种冲突。 You can also change the "quoting" if the delimiter doesn't work for you. 如果分隔符对您不起作用,您还可以更改“引号”。

Check out this link for more info: Pandas: use to_csv() with quotation marks and a comma as a seperator 查看此链接以获取更多信息: 熊猫:使用带引号和逗号作为分隔符的to_csv()

If you have this same problem, perhaps it will save you some headache by checking in here . 如果您遇到同样的问题,也许可以通过在此处签入减轻您的头痛。

None of the solutions posted there could solve my problem, so here is the code to parse the string and convert it to the format I need: 那里发布的所有解决方案都无法解决我的问题,因此这是解析字符串并将其转换为所需格式的代码:

   df = pd.read_csv(filepath_or_buffer = path_x,
                       header = None, 
                       names = ["i", "clauses"]) 

    #it is sad that I have to do that!
    df["clauses"] = df["clauses"].apply(lambda x: x.replace("[", ""))
    df["clauses"] = df["clauses"].apply(lambda x: x.replace("]", ""))
    df["clauses"] = df["clauses"].apply(lambda x: x.replace("\n", ""))
    df["clauses"] = df["clauses"].apply(lambda x: x.replace(",", ""))
    df["clauses"] = df["clauses"].apply(lambda x: x.split(" "))
    df["clauses"] = df["clauses"].apply(lambda x: np.array([int(i) for i in x]))

    cols = [x for x in range(120060)]
    df_x = pd.DataFrame(columns = cols)

    for i in range(len(df)):   
        df_x = df_x.append(pd.Series(data = {k: df["clauses"][i][k] for k in cols}),
                           ignore_index = True)

    df = pd.read_csv(filepath_or_buffer = path_y,
                       header = None, 
                       names = ["i", "label"]) 

    df_x.astype("int")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM