Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning index column to empty pandas dataframe

I am creating an empty dataframe that i then want to add data to one row at a time. I want to index on the first column, 'customer_ID'

I have this:

In[1]: df = pd.DataFrame(columns = ['customer_ID','a','b','c'],index=['customer_ID'])
In[2]: df
Out[3]: 
            customer_ID    a    b    c
customer_ID         NaN  NaN  NaN  NaN

So there is already a row of NaN that I don't want. Can I point the index to the first column without adding a row of data?

like image 720
doctorer Avatar asked Feb 05 '23 17:02

doctorer


2 Answers

The answer, I think, as hinted at by @JD Long is to set the index in a seprate instruction:

In[1]: df = pd.DataFrame(columns = ['customer_ID','a','b','c'])
In[2]: df.set_index('customer_ID',inplace = True)
In[3]: df
Out[3]: 
Empty DataFrame
Columns: [customer_ID, a, b, c]
Index: []

I can then add rows:

In[4]: id='x123'
In[5]: df.loc[id]=[id,4,5,6]
In[6]: df
Out[7]: 
 customer_ID    a    b    c
x123        x123  4.0  5.0  6.0
like image 195
doctorer Avatar answered Feb 08 '23 06:02

doctorer


yes... and you can dropna at any time if you are so inclined:

df = df.set_index('customer_ID').dropna()
df
like image 26
JD Long Avatar answered Feb 08 '23 07:02

JD Long