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, 2019 in Python by ana1504.k
• 7,900 points
531 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, 2019 by SDeb
• 13,270 points

Related Questions In Python

0 votes
1 answer

How to add multiple columns to pandas dataframe in one assignment?

Hello @kartik, You could use assign with a dict of ...READ MORE

answered Jun 15, 2020 in Python by Niroj
• 82,680 points
7,692 views
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, 2019 in Python by Raj
1,373 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, 2019 in Python by Taj
• 1,080 points
4,434 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, 2019 in Python by Suri
5,957 views
0 votes
1 answer

How to rename columns in pandas (Python)?

You can use the rename function in ...READ MORE

answered Apr 30, 2018 in Data Analytics by DeepCoder786
• 1,720 points

edited Jun 8, 2020 by MD 627 views
0 votes
1 answer

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

The major difference is "size" includes NaN values, ...READ MORE

answered Apr 30, 2018 in Data Analytics by DeepCoder786
• 1,720 points

edited Jun 8, 2020 by Gitika 1,433 views
0 votes
2 answers

Replacing a row in pandas data.frame

key error. I love python READ MORE

answered Feb 18, 2019 in Data Analytics by anonymous
8,520 views
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,620 points
3,482 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,270 points
151 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, 2019 in Python by SDeb
• 13,270 points
1,073 views