Add one row to pandas DataFrame

0 votes
I know that pandas is designed to load fully populated DataFrame but what should I do if I need to create an empty DataFrame and then add rows, one by one. Can anyone help me with the best possible way?

I successfully created an empty DataFrame with :

res = DataFrame(columns=('lib', 'qty1', 'qty2'))
Then I can add a new row and fill a field with :

res = res.set_value(len(res), 'qty1', 10.0)
 

How can I add a new row to my DataFrame with different columns type ?
Mar 1 in Python by ana1504.k
• 7,870 points
220 views

1 answer to this question.

0 votes
You can try the following :

>>> import pandas as pd
>>> import numpy as np
>>> df = pd.DataFrame(columns=['lib', 'qty1', 'qty2'])
>>> for i in range(5):
>>>     df.loc[i] = [np.random.randint(-1,1) for n in range(3)]
>>>
>>> print(df)
    lib  qty1  qty2
0    0     0    -1
1   -1    -1     1
2    1    -1     1
3    0     0     0
4    1    -1    -1

[5 rows x 3 columns]

Hope this works!
answered Mar 1 by SDeb
• 13,180 points

Related Questions In Python

0 votes
1 answer

How to append a row to a Pandas dataframe?

You can use the append method provided by pandas ...READ MORE

answered May 9 in Python by Raj
259 views
0 votes
1 answer

How to create a train and test sample from one dataframe using pandas?

Hi, The below written code can help you ...READ MORE

answered Jul 3 in Python by Taj
• 1,040 points
283 views
0 votes
1 answer

Add header row in pandas dataframe while printing

Refer to this code: import pandas as pd col_name=['Name', ...READ MORE

answered Jul 5 in Python by Suri
714 views
0 votes
1 answer

How to iterate over row in a Dataframe in Pandas?

Hi, You can use df.iterrows(), it yields both ...READ MORE

answered Jul 18 in Python by Taj
• 1,040 points
54 views
0 votes
1 answer

How to rename columns in pandas (Python)?

It is easy by just adding ".columns" ...READ MORE

answered Apr 30, 2018 in Data Analytics by DeepCoder786
• 1,720 points
134 views
0 votes
1 answer

What is the Difference in Size and Count in pandas (python)?

The major difference is size includes NaN ...READ MORE

answered Apr 30, 2018 in Data Analytics by DeepCoder786
• 1,720 points
754 views
0 votes
2 answers
0 votes
1 answer

Converting a pandas data-frame to a dictionary

Emp_dict=Employee.to_dict('records') You can directly use the 'to_dict()' function ...READ MORE

answered May 23, 2018 in Data Analytics by Bharani
• 4,550 points
1,632 views
0 votes
1 answer

How do I append one string to another in Python?

If you only have one reference to ...READ MORE

answered Oct 22, 2018 in Python by SDeb
• 13,180 points
49 views
0 votes
1 answer

Round columns in pandas dataframe

You can now, use round on dataframe The code will ...READ MORE

answered Jul 4 in Python by SDeb
• 13,180 points
80 views