在python中上传CSV文件的问题

发布于 2025-01-18 07:32:23 字数 463 浏览 4 评论 0原文

我在 python 中上传 excel 数据时遇到一些问题。

Excel

image

用于上传的代码

import pandas as pd
from google.colab import files
#uploaded = files.upload()
import io
df2 = pd.read_csv(io.BytesIO(uploaded['nodes.csv']),index_col=0)
print (df2)

结果:

image

你能帮我吗?

I have some problem with uploding excel data in python.

Excel:

image

Code used to upload:

import pandas as pd
from google.colab import files
#uploaded = files.upload()
import io
df2 = pd.read_csv(io.BytesIO(uploaded['nodes.csv']),index_col=0)
print (df2)

Result:

image

Can you kindly help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你又不是我 2025-01-25 07:32:24

您说要将导入的数据转换为numpy阵列,可以通过使用以下方式而无需使用pandas:

import numpy as np
arr = np.genfromtxt('nodes.csv', delimiter=',')
print(arr)

检查文档: https://numpy.org/doc/stable/reference/generated/generated/numpy.genfromtxt.htxt.html

You said you wanted to convert the imported data to a numpy array, you could do so by doing the following without using pandas:

import numpy as np
arr = np.genfromtxt('nodes.csv', delimiter=',')
print(arr)

Check the documentation: https://numpy.org/doc/stable/reference/generated/numpy.genfromtxt.html

昵称有卵用 2025-01-25 07:32:24

如果您有一个CSV-File file.csv

0,0
2,0
4,0
1,1.732051
3,1.732051

df = pd.read_csv("file.csv", index_col=0)

确实会产生

df =
        0.1
0          
2  0.000000
4  0.000000
1  1.732051
3  1.732051

为什么:第一行中有两个0 s,而PANDAS则是 mange dupes /em>因为该行用于标签。 0.1不是数字,它是字符串(print(df.columns)将显示index(['0.1'],dtype ='对象' ))。如果您的文件看起来像是

0,1
2,0
4,0
1,1.732051
3,1.732051

输出将

          1
0          
2  0.000000
4  0.000000
1  1.732051
3  1.732051

这样,那将不会发生,如果您的目标是Numpy数组,则

arr = pd.read_csv("file.csv", header=None).values

导致

array([[0.      , 0.      ],
       [2.      , 0.      ],
       [4.      , 0.      ],
       [1.      , 1.732051],
       [3.      , 1.732051]])

If you have a csv-file file.csv

0,0
2,0
4,0
1,1.732051
3,1.732051

then

df = pd.read_csv("file.csv", index_col=0)

does produce

df =
        0.1
0          
2  0.000000
4  0.000000
1  1.732051
3  1.732051

Why is that: There are two 0s in the first row and Pandas is mangling the dupes because the row is used for labels. The 0.1 isn't a number, it's a string (print(df.columns) will show Index(['0.1'], dtype='object')). If your file would look like

0,1
2,0
4,0
1,1.732051
3,1.732051

then this wouldn't happen, the output would look like

          1
0          
2  0.000000
4  0.000000
1  1.732051
3  1.732051

If your goal is NumPy array, then

arr = pd.read_csv("file.csv", header=None).values

leads to

array([[0.      , 0.      ],
       [2.      , 0.      ],
       [4.      , 0.      ],
       [1.      , 1.732051],
       [3.      , 1.732051]])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文