4

I want to dynamically extend an empty pandas DataFrame in the following way:

df=pd.DataFrame()
indices=['A','B','C']
colums=['C1','C2','C3']
for colum in colums:
    for index in indices:
        #df[index,column] = anyValue

Where both indices and colums can have arbitrary sizes which are not known in advance, i.e. I cannot create a DataFrame with the correct size in advance.

Which pandas function can I use for

#df[index,column] = anyValue

?

2 Answers 2

3

I think you can use loc:

df = pd.DataFrame()

df.loc[0,1] = 10
df.loc[2,8] = 100
print(df)
      1      8
0  10.0    NaN
2   NaN  100.0

Faster solution with DataFrame.set_value:

df = pd.DataFrame()
indices = ['A', 'B', 'C']
columns = ['C1', 'C2', 'C3']
for column in columns:
    for index in indices:
       df.set_value(index, column, 1)

print(df)
    C1   C2   C3
A  1.0  1.0  1.0
B  1.0  1.0  1.0
C  1.0  1.0  1.0
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, that would be possible - but I need the indices and the column strings. Is there any variation of loc such that the resulting df would contain the indices 'A','B' etc and the column names 'C1','C2' ?
2

loc works very well, but...
For single assignments use at

df = pd.DataFrame()
indices = ['A', 'B', 'C']
columns = ['C1', 'C2', 'C3']
for column in columns:
    for index in indices:
        df.at[index, column] = 1

df

enter image description here


.at vs .loc vs .set_value timing

enter image description here

3 Comments

Can you add timings for set_value?
One little complaint, though: The order of column and index seems to be wrong in df.at. it should be df.at[index,column] - otherwise: Thanks for your help!
@user1934212 fixed

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.