简体   繁体   English

如何明智地读取csv行并使用pandas转换为dataframe列?

[英]How to read a csv row wise and convert to a dataframe column using pandas?

My dataset CSV file is row-wise: 我的数据集CSV文件是逐行的:

1,2,3,4
1000,2000,3000,4000

I want to read this file and get a dataframe output of two columns, 'index and value'. 我想读取此文件并获取两列“索引和值”的数据框输出。

Output: 输出:

index value
1      1000
2      2000
3      3000

If I want to get only the 'value' column, I should be able to retrieve them by doing df['value'] 如果我只想获取“值”列,则应该可以通过执行df['value']来检索它们

I tried going about it like this: 我试图这样做:

series = pd.read_csv('file.csv',index_col=0, header=0)
df= series.T
Frame=pd.DataFrame([df], columns = ["index","value"])

But this yields an error: 但这会产生一个错误:

> ValueError: Shape of passed values is (1, 1), indices imply (2, 1)

try this, 尝试这个,

df=pd.read_csv('input.csv',header=None)
print df.T.rename(columns={0:'Index',1:'Value'})
print df['Value']

Out: 日期:

0    1000
1    2000
2    3000
3    4000
Name: Value, dtype: int64

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

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