1

i create a csv file from a pandas dataframe with lots of columns for our ERP system. Most of the columns are empty or constant, but the importtool of the system expects a csv file with a specified column order.

When creating the dataframe, i have something like that:

importlist = pd.DataFrame({ 'A': 'TestA',
                            'C': some values,
                            'B': '0'})

When i print the list, the columns are ordered A, B, C.

Is there a way to prevent automatical ordering? I know i could sort the columns manually again, but i think, this would't be a very nice solution, because there are about 50 columns.

2
  • Do you really create a dataframe like this or read data source from text file or database? Commented Jan 13, 2017 at 14:07
  • I have to create a DataFrame like this. This is a bill of material for pcbs which are imported in our ERP system. Commented Jan 13, 2017 at 18:23

2 Answers 2

3

For completeness, you can also use an OrderedDict which you pass a list of two element tuples where the first element of the tuple is the column name and the second element is a list of values.

pd.DataFrame(OrderedDict([('A',['TestA', 'TestB']), 
                          ('C', ['Some Item', 'Other Item']),
                         ('B',[0, 1])]))

       A           C  B
0  TestA   Some Item  0
1  TestB  Other Item  1
Sign up to request clarification or add additional context in comments.

Comments

1

You need pass parameter columns with list of your columns of desired order, because default python dict is un-ondered:

importlist = pd.DataFrame({ 'A': 'TestA',
                        'C': some values,
                        'B': '0'}, columns = ['A','C','B'])

Sample:

importlist = pd.DataFrame({ 'A': ['TestA'],
                            'C':  [4],
                            'B': ['0']}, columns = ['A','C','B'])

print (importlist)
       A  C  B
0  TestA  4  0

Or use read_csv for reading csv if possible.

In docs are another methods for creating DataFrames, one of them is DataFrame.from_items:

a = ['TestA','TestB']
b = ['0', '7']
c = [4,7]

print (pd.DataFrame.from_items([('A', a), ('C', c),('B', b)]))
       A  C  B
0  TestA  4  0
1  TestB  7  7

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.