Problem when creating Dataframe in Pandas using Python 3.x

0

I have several lists and I want to create only one Dataframe to export later.

The lists are of the type:

    a=[1, 2, 3, 4, 5, 6, 7, 8]
    b=[9, 10, 11, 12, 13, 14, 15, 16]

For this I am creating 2 dataframes in the following way:

    export=pd.DataFrame(a, columns=['A'])
    export2=pd.DataFrame(b, columns=['B'])

My problem is that I have many lists and I would like to make only one Dataframe in this way:

    export=pd.DataFrame(a, b, columns=['A', 'B'])

But I miss an error, dimension:

    ValueError: Shape of passed values is (1, 600), indices imply (2, 576)
    
asked by Jorge Ponti 09.08.2017 в 00:31
source

1 answer

2

It does not work because you are passing a list that is somehow like an array of 8 columns and a row, your Dataframe has 2 columns 'A' and 'B', so there is no match of dimensions.

Here are several ways to do the same:

# Usando a y b con pandas:
pd.DataFrame([a,b],index=['A','B']).transpose()

# mediante pandas usando export y export2:
pd.concat([export,export2],axis=1,join='inner')

# mediante pandas version 2:
export.join(export2)

# mediante numpy
pd.DataFrame(np.hstack((export,export2)),columns=['A','B'])

EDIT: proposed by @kikocorreoso.

# mediante un dict de listas, 

pd.DataFrame(('A':a,'B':b})

The interesting thing is if the data you have, you need to join them by some criterion of the table itself.

    
answered by 09.08.2017 / 09:24
source