简体   繁体   English

如何在python中拆分没有分隔符逗号的数组并适合行csv

[英]how to split array without separator comma in python and fit to row csv

i had an output array without sepator comma only seperate with space, when i convert to pandas dataframe the array not split to any row, just stuck in 1 row我有一个没有分隔符逗号的输出数组,只用空格分隔,当我转换为 pandas 数据帧时,数组没有拆分到任何行,只是卡在 1 行

the output off arayy looks like this arayy 的输出看起来像这样

pred_ct = [217.769 228.838 238.459 225.317 196.812 221.241 214.605 205.918 206.278
 216.028 234.919 224.371 248.084 227.354 231.194 206.726 215.848 238.627
 239.545 246.615 231.238 263.073 249.877 222.093 231.18  242.29  253.832
 211.224 222.574 232.379 218.966 189.899 214.793 208.07  199.198 199.549
 209.464 228.733 218.001 242.169 220.975 224.912 199.978 209.294 232.528
 233.393 240.55  224.901 257.376 243.908 215.563 224.834 236.128 247.95
 204.479 216.109 226.098 212.414 182.785 208.144 201.334 192.279 192.62
 202.701 222.347 211.431 236.054 214.395 218.429 193.03  202.54  226.229
 227.04  234.284 218.365 251.478 237.74  208.833 218.287 229.766 241.869]

pred_ra = [0.54  0.992 1.043 0.718 0.478 0.946 0.678 0.642 0.507 0.499 0.717 0.681
 1.056 0.527 0.969 0.476 0.65  1.01  0.676 0.99  0.652 1.064 0.705 0.463
 0.48  0.502 1.025 0.528 0.984 1.035 0.709 0.465 0.937 0.668 0.631 0.494
 0.487 0.707 0.671 1.049 0.516 0.96  0.463 0.639 1.002 0.666 0.981 0.641
 1.057 0.696 0.45  0.468 0.491 1.017 0.516 0.976 1.028 0.698 0.452 0.928
 0.657 0.619 0.482 0.474 0.697 0.66  1.041 0.504 0.951 0.449 0.628 0.994
 0.655 0.972 0.629 1.049 0.686 0.437 0.456 0.479 1.009]

and i use thisanother code and had no different我使用thisanother代码并没有什么不同

import pandas as pd
import numpy as np

datapredct0 = np.array(pred_ct)
datapredct1 = {'CUTTING TEMPERATURE' : [datapredct0]}
datapredct2 = pd.DataFrame(datapredct1, columns=['CUTTING TEMPERATURE'])

datapredra0 = np.array(pred_ra)
datapredra1 = {'SURFACE ROUGHNESS' : [datapredra0]}
datapredra2 = pd.DataFrame(datapredra1, columns=['SURFACE ROUGHNESS'])

alldatapred = pd.concat([datapredct2, datapredra2], axis=1, join='inner')

alldatapred.to_csv('alldata.csv', 
            sep=",",  # char used as separator between columns
            index=False, # do not save index column
            header=True  # do save headers
        )

i not understand to fix the diferent sepator, if i delete comma to this如果我删除逗号,我不明白要修复不同的分隔符

sep=" "

it make other coloumn merge in one coloumn like它使其他列合并到一个列中

for the preview csv file like this对于这样的预览 csv 文件

它的拆分列,但数据未拆分到另一行

Don't focus so much on the comma separator.不要过分关注逗号分隔符。 Pay more attention to what kinds of objects you produce.更加注意您生产的对象类型。 Things like [], are display indicators. [],之类的东西是显示指示器。

Lets make a simple 1d array:让我们做一个简单的一维数组:

In [2]: x = np.arange(10)
In [3]: x
Out[3]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [4]: print(x)
[0 1 2 3 4 5 6 7 8 9]

It can be display 2 different ways, the repr and str (as with all python classes).它可以显示 2 种不同的方式, reprstr (与所有 python 类一样)。 The missing commas in the last distinguish this from a Python list.最后一个中缺少的逗号将其与 Python 列表区分开来。 Commas or not is a display feature.逗号与否是一个显示功能。

Calling array on it doesn't change anything:在它上面调用array不会改变任何东西:

In [5]: np.array(x)
Out[5]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

If I make a dataframe with it:如果我用它制作一个数据框:

In [6]: df = pd.DataFrame(x, columns=['foo'])
In [7]: df
Out[7]: 
   foo
0    0
1    1
2    2
3    3
4    4
5    5
6    6
7    7
8    8
9    9

But you make a frame via a dict :但是您通过dict制作框架:

In [9]: df = pd.DataFrame({'foo':[x]}, columns=['foo'])
In [10]: df
Out[10]: 
                              foo
0  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

This is a 1 row, 1 column frame, with a numpy array in the only cell.这是一个 1 行 1 列的框架,在唯一的单元格中有一个 numpy 数组。 In the Pandas display style, it is hard to tell whether this cell is an array, a list, or a string.在 Pandas 显示样式中,很难判断此单元格是数组、列表还是字符串。

If we write this array to csv, we get:如果我们将此数组写入 csv,我们会得到:

In [16]: df.to_csv('foo.txt',sep=",",index=False,header=True)
In [17]: cat foo.txt
foo
[0 1 2 3 4 5 6 7 8 9]

csv is a 2d format, so it can't display the array cell value as a comma separated column. csv 是 2d 格式,因此它不能将数组单元格值显示为逗号分隔的列。 Instead it just writes the str(x) format, as in my [4].相反,它只编写str(x)格式,如我的 [4] 中所示。 The sep parameter applies to columns of the frame, not to arrays within individual columns. sep参数适用于框架的列,而不是单个列中的数组。

If I load that file back into a dataframe, I get something that looks a lot like the df , except for the commas:如果我将该文件加载回数据框中,我会得到一些看起来很像df的东西,除了逗号:

In [18]: df1 = pd.read_csv('foo.txt')
In [19]: df1
Out[19]: 
                     foo
0  [0 1 2 3 4 5 6 7 8 9]

The cell is a string, not a numpy array:单元格是一个字符串,而不是一个 numpy 数组:

In [21]: df['foo'][0]
Out[21]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [22]: df1['foo'][0]
Out[22]: '[0 1 2 3 4 5 6 7 8 9]'

Putting arrays in cells of a dataframe creates a frame that is hard to write as csv, and harder to reload.将数组放在数据帧的单元格中会创建一个难以写入 csv 且更难重新加载的帧。

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

相关问题 如何使用逗号分隔符和逗号分隔符读取熊猫CSV文件 - How to read pandas CSV file with comma separator and comma thousand separator 在 python 中用逗号作为小数分隔符写入 csv - Write csv with a comma as decimal separator in python 如何将基于逗号分隔的字符串拆分为双引号内的逗号分隔符,保留在python中 - How to split a string based on comma as separator with comma within double quotes remaining as it is in python 如何用逗号作为小数点分隔符编写csv? - How to write a csv with a comma as the decimal separator? Python在没有括号的逗号上拆分 - Python split on comma without parenthesis 如何在 Python 中拆分重复的分隔符 - How to split duplicated separator in Python Python:如何从导入的 csv 文件中删除数字中的逗号分隔符 - Python: How to remove comma number separator in numbers from imported csv file 如何在python中将数据行拆分为以逗号分隔的列 - How to split row of data into columns separated by comma in python 如何基于逗号拆分包含多个字符串值的 csv 行,但不考虑大括号内的逗号 { } - How to split csv rows containing multiple string values based on comma but without considering comma inside curly brackets { } 如何在 python 中的文本文件中添加逗号分隔符 - How to add a comma separator to a text file in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM