4

I have a csv file which looks like this:

-0.08150654489363679, 0.3262445628643036, -0.1983973830938339, 0.04597456371557881

and I am reading the file in like this:

import pandas as pd

df=pd.read_csv(r'F:\Sheyenne\Statistics\IDL_stats\Basic_Stats\NDII\NDII_1984137_A_Annex.csv')    
print df

which returns this:

Empty DataFrame
Columns: [-0.08150654489363679, 0.3262445628643036, -0.1983973830938339, 0.04597456371557881]
Index: []

I want to add column names to the columns like this:

df=pd.read_csv(r'F:\Sheyenne\Statistics\IDL_stats\Basic_Stats\NDII\NDII_1984137_A_Annex.csv')    
df.columns=['Mean', 'Max', 'Min', 'Stdev']
print df

but when I do this I get this:

Empty DataFrame
Columns: [Mean, Max, Min, Stdev]
Index: []

My desired output is this:

      Mean                Max                   Min               Stdev
-0.08150654489363679 0.3262445628643036 -0.1983973830938339 0.04597456371557881

something funny is going on when the dataframe is being read but Im not sure what.

1 Answer 1

2

Pass the columns names as an arg to read_csv:

df=pd.read_csv(r'F:\Sheyenne\Statistics\IDL_stats\Basic_Stats\NDII\NDII_1984137_A_Annex.csv', names=['Mean', 'Max', 'Min', 'Stdev'])    

by default it treats the first header row as the column names so when you overwrite the columns you end up with an empty df as you only had a single row in your csv in the first place.

Also it looks like your file has initial white space, you can set to skip these too:

df=pd.read_csv(r'F:\Sheyenne\Statistics\IDL_stats\Basic_Stats\NDII\NDII_1984137_A_Annex.csv', names=['Mean', 'Max', 'Min', 'Stdev'], skipinitialspace=True)
Sign up to request clarification or add additional context in comments.

Comments

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.