[英]Problem with uploading CSV file in python
I have some problem with uploding excel data in python.我在 python 中上传 excel 数据时遇到一些问题。
Excel : Excel :
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 :结果:
Can you kindly help me?你能帮帮我吗?
You said you wanted to convert the imported data to a numpy array, you could do so by doing the following without using pandas:你说你想将导入的数据转换为 numpy 数组,你可以通过执行以下操作而不使用 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查看文档: https://numpy.org/doc/stable/reference/generated/numpy.genfromtxt.html
If you have a csv-file file.csv
如果你有一个 csv 文件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 0
s in the first row and Pandas is mangling the dupes because the row is used for labels.这是为什么:第一行有两个0
并且 Pandas 正在处理欺骗,因为该行用于标签。 The 0.1
isn't a number, it's a string ( print(df.columns)
will show Index(['0.1'], dtype='object')
). 0.1
不是一个数字,它是一个字符串( print(df.columns)
将显示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那么这不会发生,output 看起来像
1
0
2 0.000000
4 0.000000
1 1.732051
3 1.732051
If your goal is NumPy array, then如果你的目标是 NumPy 数组,那么
arr = pd.read_csv("file.csv", header=None).values
leads to导致
array([[0. , 0. ],
[2. , 0. ],
[4. , 0. ],
[1. , 1.732051],
[3. , 1.732051]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.