gpt4 book ai didi

python - Pandas 数据框不根据 csv 中的逗号分隔列

转载 作者:太空宇宙 更新时间:2023-11-03 15:20:38 25 4
gpt4 key购买 nike

我想根据 CSV 数据的表格创建一个矩阵

COEFFICIENT MATRIX 
,0,1,2,3,4
0,0.00876623398408,0.525189723661,0.528495953628,0.94228622319,0.0379073884588
1,0.434693398364,0.77017930965,0.00847865052462,0.544319471939,0.858970329817
2,0.978091233581,0.900800004769,0.504567295427,0.65499490009,0.397203736755
3,0.671510258373,0.554713361673,0.377098128478,0.246977226206,0.535900353082
...
5000,0.791781572037,0.70262685963,0.218775600741,0.19802280762,0.68177855465

我正在使用 pandas 读取 csv 并返回一个矩阵。我没有得到matrix.shape = 5001*5,而是得到了5002*1。

如何使 pandas dataframe 根据 CSV 中的逗号分隔正确的列数,并且不将标题(表格标题之后)计为第一行?

 input = pd.read_csv(coeff_file, skiprows=0)
input_mat = input.as_matrix()

print input.shape
print type(input)

print input_mat.shape
print type(input_mat)

返回

(5002, 1)
<class 'pandas.core.frame.DataFrame'>
(5002, 1)
<type 'numpy.ndarray'>

最佳答案

我认为 read_csv 中需要 skiprows=1skiprows=[0]header=1 参数:

df = pd.read_csv(coeff_file, skiprows=1, index_col=0)
print (df)
0 1 2 3 4
0 0.008766 0.525190 0.528496 0.942286 0.037907
1 0.434693 0.770179 0.008479 0.544319 0.858970
2 0.978091 0.900800 0.504567 0.654995 0.397204
3 0.671510 0.554713 0.377098 0.246977 0.535900
5000 0.791782 0.702627 0.218776 0.198023 0.681779
<小时/>
df = pd.read_csv(coeff_file, header=1, index_col=0)
print (df)
0 1 2 3 4
0 0.008766 0.525190 0.528496 0.942286 0.037907
1 0.434693 0.770179 0.008479 0.544319 0.858970
2 0.978091 0.900800 0.504567 0.654995 0.397204
3 0.671510 0.554713 0.377098 0.246977 0.535900
5000 0.791782 0.702627 0.218776 0.198023 0.681779
<小时/>
df = pd.read_csv(StringIO(temp), skiprows=[0], index_col=0)
print (df)
0 1 2 3 4
0 0.008766 0.525190 0.528496 0.942286 0.037907
1 0.434693 0.770179 0.008479 0.544319 0.858970
2 0.978091 0.900800 0.504567 0.654995 0.397204
3 0.671510 0.554713 0.377098 0.246977 0.535900
5000 0.791782 0.702627 0.218776 0.198023 0.681779

关于python - Pandas 数据框不根据 csv 中的逗号分隔列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43580771/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com