1

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)

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

11 Comments

Throwing a KeyError: 'Value' when I print df['Value'].
@AmoghMishra - I'm not getting any key error. can you show your df before accessing df['Value']? this may rename doesn't work as expected
yeah, my df is not returning a similar result. I don't get Name: Value, dtype: int64 in the end. Just to add more information, shape of df before transposing is (1,166) and after transposing (166,1)
@AmoghMishra- shape after transpose it will be like that only. It would be great if you print the result after transpose.
This is the output of the df that I am getting 0 1000 1 2000 2 3000 3 4000 ... ... 166 166000 [166 rows x 1 columns]
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.