Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas- set values to an empty dataframe

Tags:

python

pandas

I have initialized an empty pandas dataframe that I am now trying to fill but I keep running into the same error. This is the (simplified) code I am using

import pandas as pd
cols = list("ABC")
df = pd.DataFrame(columns=cols)
# sett the values for the first two rows
df.loc[0:2,:] = [[1,2],[3,4],[5,6]]

On running the above code I get the following error:

ValueError: cannot copy sequence with size 3 to array axis with dimension 0

I am not sure whats causing this. I tried the same using a single row at a time and it works (df.loc[0,:] = [1,2,3]). I thought this should be the logical expansion when I want to handle more than one rows. But clearly, I am wrong. Whats the correct way to do this? I need to enter values for multiple rows and columns and once. I can do it using a loop but that's not what I am looking for.

Any help would be great. Thanks

like image 677
Clock Slave Avatar asked Apr 23 '26 04:04

Clock Slave


2 Answers

Since you have the columns from empty dataframe use it in dataframe constructor i.e

import pandas as pd
cols = list("ABC")
df = pd.DataFrame(columns=cols)

df = pd.DataFrame(np.array([[1,2],[3,4],[5,6]]).T,columns=df.columns) 

   A  B  C
0  1  3  5
1  2  4  6

Well, if you want to use loc specifically then, reindex the dataframe first then assign i.e

arr = np.array([[1,2],[3,4],[5,6]]).T
df = df.reindex(np.arange(arr.shape[0]))
df.loc[0:arr.shape[0],:] = arr

   A  B  C
0  1  3  5
1  2  4  6
like image 155
Bharath Avatar answered Apr 25 '26 17:04

Bharath


How about adding data by index as below. You can add externally to a function as and when you receive data.

def add_to_df(index, data):
    for idx,i in zip(index,(zip(*data))):
        df.loc[idx]=i

#Set values for first two rows
data1 = [[1,2],[3,4],[5,6]]
index1 = [0,1]
add_to_df(index1, data1)
print df
print ""

#Set values for next three rows
data2 = [[7,8,9],[10,11,12],[13,14,15]]
index2 = [2,3,4]
add_to_df(index2, data2)
print df

Result

>>> 
     A    B    C
0  1.0  3.0  5.0
1  2.0  4.0  6.0

     A     B     C
0  1.0   3.0   5.0
1  2.0   4.0   6.0
2  7.0  10.0  13.0
3  8.0  11.0  14.0
4  9.0  12.0  15.0
>>> 
like image 43
Anil_M Avatar answered Apr 25 '26 18:04

Anil_M