简体   繁体   中英

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

My dataset CSV file is row-wise:

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']

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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