Creating an empty Pandas DataFrame
Create an empty DataFrame :
import pandas as pd
import numpy as np
df = pd.DataFrame()
print(df)
Empty DataFrame
Columns: []
Index: []
Add columns to an empty DataFrame
import pandas as pd
import numpy as np
df = pd.DataFrame()
df['Name'] = ['John', 'Doe', 'Bill']
df['Promoted'] = [True, False,True]
df['Marks'] = [82, 38, 63]
print(df)
Name Promoted Marks
0 John True 82
1 Doe False 38
2 Bill True 63
Method 2 Initialize empty frame with column names
import pandas as pd
import numpy as np
col_names = ['Name', 'Promoted', 'Marks']
df = pd.DataFrame(columns = col_names)
df
Empty DataFrame
Columns: [Name, Promoted, Marks]
Index: []
Add a new record to a dataframe
import pandas as pd
import numpy as np
col_names = ['Name', 'Promoted', 'Marks']
df = pd.DataFrame(columns = col_names)
df.loc[len(df)] = ['John', True, 80]
df
Name Promoted Marks
0 John True 80
Using Dictionary to add data in a dataframe
import pandas as pd
import numpy as np
col_names = ['Name', 'Promoted', 'Marks']
df = pd.DataFrame(columns = col_names)
my_dic = {'Name':'John', 'Promoted':True, 'Marks':80}
df.loc[len(df)] = my_dic
df
Name Promoted Marks
0 John True 80

Using append() method to add data
import pandas as pd
import numpy as np
df = pd.DataFrame()
data = pd.DataFrame({"A": range(5)})
df.append(data)
A
0 0
1 1
2 2
3 3
4 4
Create pandas Dataframe by appending one row at a time
In this case, you can use DataFrame.loc[index] , where the row with index will be what you specify it to be in the dataframe.
import pandas as pd
from numpy.random import randint
df = pd.DataFrame(columns=['Student', 'Mark1', 'Mark2'])
for i in range(5):
df.loc[i] = ['Student-' + str(i)] + list(randint(10, size=2))
df
Student Mark1 Mark2
0 Student-0 3 7
1 Student-1 4 2
2 Student-2 4 6
3 Student-3 3 0
4 Student-4 1 6
Related Topics
- How to Check if a Pandas DataFrame is Empty
- How to check if a column exists in Pandas Dataframe
- How to delete column from pandas DataFrame
- How to select multiple columns from Pandas DataFrame
- Selecting multiple columns in a Pandas dataframe based on condition
- Selecting rows in pandas DataFrame based on conditions
- How to Drop rows in DataFrame by conditions on column values
- Rename column in Pandas DataFrame
- Get a List of all Column Names in Pandas DataFrame
- How to add new columns to Pandas dataframe?
- Change the order of columns in Pandas dataframe
- Concatenate two columns into a single column in pandas dataframe
- How to count the number of rows and columns in a Pandas DataFrame
- Use a list of values to select rows from a pandas dataframe
- How to iterate over rows in a DataFrame in Pandas
- How to drop rows/columns of Pandas DataFrame whose value is NaN
- How to Export Pandas DataFrame to a CSV File
- Convert list of dictionaries to a pandas DataFrame
- How to set a particular cell value in pandas DataFrame