简体   繁体   English

熊猫数据框无法识别索引

[英]pandas data frame not recognizing index

I'm pretty new to python, and am trying to read in a single row of data to a data frame, and then index it by value to get occurrence counts for each value in the row. 我是python的新手,正在尝试将单行数据读入数据帧,然后按值对它进行索引以获得该行中每个值的出现次数。 This is my code so far: 到目前为止,这是我的代码:

import pandas as pd
csv=pd.read_csv('filepath/data.csv', 'r', converters={'csv':str})
df=DataFrame(csv, columns=['data'], index=['0.0', '750.0'])
df

When I just view 'csv' after reading in, it looks like this: 当我读完后仅查看“ csv”时,它看起来像这样:

0.0 750.0 750.0 750.0 750.0 750.0 750.0

When I attempt to input it to a data frame however, I get this result: 但是,当我尝试将其输入到数据帧时,得到以下结果:

data
0.0   NaN
750.0 NaN

What I'm hoping to get: 我希望得到的是:

data
0.0   1
750.0 6

Thanks in advance for any insight! 预先感谢您的任何见解!

Pandas read_csv is designed for tabular data with multiple rows and columns: if your data file has only a single row of values, it is probably cleaner to read it directly using Python's open() . Pandas read_csv设计用于具有多行和多列的表格数据:如果您的数据文件只有一行值,那么使用Python的open()直接读取它可能更干净。 Once you have those results in a list, pandas value_counts method will give you the counts of each value in the list: eg 将这些结果放入列表后,pandas value_counts方法将为您提供列表中每个值的计数:例如

values = open('data.csv').read().split()
pd.Series(values).value_counts()
# 750.0    6
# 0.0      1
# dtype: int64

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

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