0

I am getting Wrong column names by this code: Why is this happening? Is there any other way to do this?

names = {'SYMBOL','CLMP'}
data2 = pd.read_csv(filePath +"\\"+ fileNameCM2+'.csv',skiprows = 1, index_col=False, names = names,header=None)
df3 = pd.DataFrame(data2)

print(df3.head())

OUTPUT:
          CLMP    SYMBOL
0         ACC    853921
1    ADANIENT   2758466
2  ADANIPORTS   7591819
3  ADANIPOWER  11774513
4  AJANTPHARM    257752

EXPECTED:
      SYMBOL      CLMP
0         ACC    853921
1    ADANIENT   2758466
2  ADANIPORTS   7591819
3  ADANIPOWER  11774513
4  AJANTPHARM    257752
0

1 Answer 1

1

Its because set will sort the names, instead use a list of columns names:

names = ['SYMBOL','CLMP']
df3 = pd.read_csv(filePath +"\\"+ fileNameCM2+'.csv',
                  skiprows = 1, 
                  index_col=False, 
                  names = names)

print (df)
       SYMBOL      CLMP
0         ACC    853921
1    ADANIENT   2758466
2  ADANIPORTS   7591819
3  ADANIPOWER  11774513
4  AJANTPHARM    257752
Sign up to request clarification or add additional context in comments.

3 Comments

Yes working fine, but what was {'SYMBOL','CLMP'} this doing?
I think it is set, so order should be changed.
I thought OP was trying to sort the columns giving the names, didn't realise he was trying to change the names

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.