Here is another solution using Index.repeat
:
df.loc[df.index.repeat(3)]
With np.repeat
(no need to reshape
in this case as opposed to suggested np.tile
solution):
pd.DataFrame(pd.np.repeat(df.values, 3, axis = 0), columns = df.columns)
March 7, 2022March 7, 2022
To follow along with this tutorial line-by-line, you can copy the code below into your favourite code editor. If you have your own data to follow along with, feel free to do so (though your results will, of course, vary):
# Loading a Sample Pandas DataFrame import pandas as pd df = pd.DataFrame.from_dict({ 'Name': ['Nik', 'Kate', 'Evan', 'Kyra'], 'Age': [31, 30, 40, 33], 'Location': ['Toronto', 'London', 'Kingston', 'Hamilton'] }) print(df) # Returns: # Name Age Location # 0 Nik 31 Toronto # 1 Kate 30 London # 2 Evan 40 Kingston # 3 Kyra 33 Hamilton
We could simply write:
df = df.append({ 'Name': 'Jane', 'Age': 25, 'Location': 'Madrid' }, ignore_index = True) print(df) # Returns: # Name Age Location # 0 Nik 31 Toronto # 1 Kate 30 London # 2 Evan 40 Kingston # 3 Kyra 33 Hamilton # 4 Jane 25 Madrid
To add a list to a Pandas DataFrame works a bit differently since we can’t simply use the .append()
function. In order to do this, we need to use the loc
accessor. The label that we use for our loc
accessor will be the length of the DataFrame. This will create a new row as shown below:
df.loc[len(df)] = ['Jane', 25, 'Madrid'] print(df) # Returns: # Name Age Location # 0 Nik 31 Toronto # 1 Kate 30 London # 2 Evan 40 Kingston # 3 Kyra 33 Hamilton # 4 Jane 25 Madrid
However, we must first create a DataFrame. We can do this using the pd.DataFrame()
class. Let’s take a look:
df = pd.DataFrame([ ['Jane', 25, 'Madrid'] ], columns = df.columns).append(df) print(df) # Returns: # Name Age Location # 0 Jane 25 Madrid # 0 Nik 31 Toronto # 1 Kate 30 London # 2 Evan 40 Kingston # 3 Kyra 33 Hamilton
For example, if we have current indices from 0-3 and we want to insert a new row at index 2, we can simply assign it using index 1.5. Let’s see how this works:
# Inserting a Row at a Specific Index df.loc[1.5] = ['Jane', 25, 'Madrid'] df = df.sort_index().reset_index(drop = True) print(df) # Returns: # Name Age Location # 0 Nik 31 Toronto # 1 Kate 30 London # 2 Jane 25 Madrid # 3 Evan 40 Kingston # 4 Kyra 33 Hamilton
In this article, we will discuss how to add / append a single or multiple rows in a dataframe using dataframe.append() or loc & iloc.,We can pass a list of series too in the dataframe.append() for appending multiple rows in dataframe. For example, we can create a list of series with same column names as dataframe i.e.,We can add a row at specific position too in the dataframe using iloc[] attribute. Checkout the example, where we will add a list as the 3rd row the dataframe. For example,,It will append a new row to the dataframe with index label ‘k’. Let’s see a complete example to append a list as row to the dataframe,
Pandas Dataframe provides a function dataframe.append() to add rows to a dataframe i.e.
DataFrame.append(other, ignore_index = False, verify_integrity = False, sort = None)
Name Age City Country
a jack 34 Sydeny Australia
b Riti 30 Delhi India
c Vikas 31 Mumbai India
d Neelu 32 Bangalore India
e John 16 New York US
f Mike 17 las vegas US
Let’s add a new row in above dataframe by passing dictionary i.e.
# Pass the row elements as key value pairs to append() function mod_df = df.append({ 'Name': 'Sahil', 'Age': 22 }, ignore_index = True) print('Modified Dataframe') print(mod_df)
Complete example to add a dictionary as row to the dataframe is as follows,
import pandas as pd # List of Tuples students = [('jack', 34, 'Sydeny', 'Australia'), ('Riti', 30, 'Delhi', 'India'), ('Vikas', 31, 'Mumbai', 'India'), ('Neelu', 32, 'Bangalore', 'India'), ('John', 16, 'New York', 'US'), ('Mike', 17, 'las vegas', 'US') ] #Create a DataFrame object df = pd.DataFrame(students, columns = ['Name', 'Age', 'City', 'Country'], index = ['a', 'b', 'c', 'd', 'e', 'f']) print('Original Dataframe') print(df) # Pass the row elements as key value pairs to append() function mod_df = df.append({ 'Name': 'Sahil', 'Age': 22 }, ignore_index = True) print('Modified Dataframe') print(mod_df)
Output:
Original Dataframe
Name Age City Country
a jack 34 Sydeny Australia
b Riti 30 Delhi India
c Vikas 31 Mumbai India
d Neelu 32 Bangalore India
e John 16 New York US
f Mike 17 las vegas US
Modified Dataframe
Name Age City Country
0 jack 34 Sydeny Australia
1 Riti 30 Delhi India
2 Vikas 31 Mumbai India
3 Neelu 32 Bangalore India
4 John 16 New York US
5 Mike 17 las vegas US
6 Sahil 22 NaN NaN
As you can see, rows are in multiples of four (for each ID). I would like to do: 1) Insert two empty rows after each row 2) Copy and paste the rows to the inserted empty rows,The desired dataframe after above process should be:,and so forth. I am currently testing out some posts on stackoverflow but I keep getting stuck. Is there a way to perform the above process? Thank you!,→ Python Shopify API output formatted datetime string in django template
I have a dataframe with multiple columns that looks like:
Filename ID No class stage
A1 A1000 1 a 1
A2 A1000 1 a 1
B1 A1000 1 a 1
B2 A1000 1 a 1
A3 A1001 2 b 1
A4 A1001 2 b 1
B3 A1001 2 b 1
B4 A1001 2 b 1
The desired dataframe after above process should be:
Filename ID No class stage
A1 A1000 1 a 1
A1 A1000 1 a 1
A1 A1000 1 a 1
A2 A1000 1 a 1
A2 A1000 1 a 1
A2 A1000 1 a 1
Here is another solution using Index.repeat
:
df.loc[df.index.repeat(3)]
You can insert a list of values into a cell in Pandas DataFrame using DataFrame.at() ,DataFrame.iat(), and DataFrame.loc() methods. Each of these method takes different arguments, in this article I will explain how to use insert the list into the cell by using these methods with examples.,By using df.at(), df.iat(), df.loc[] method you can insert a list of values into a pandas DataFrame cell. I have covered this here by using these functions with a sample DataFrame.,Pandas.DataFrame.iat() method is used to update data in a DataFrame for a specified location. The location is in the format [position in the row, position in the column].,Pandas.DataFrame.loc() attribute can access a cell of rows and columns labels. By using this you can easily update the cell value with any value. In our case, will update with a list.
If you are in a hurry, below are some quick examples of how to insert a list into single or multiple cells in DataFrame.
# Below are quick examples. # Insert list into cell using df.at(). df.at[1, 'Duration'] = ['30days', '35days'] # Insert list index into cell by df.iat() method. df.iat[1, df.columns.get_loc('Duration')] = ['30days', '35days'] # Get list index into cell using df.loc() attribute. df.loc[1, 'Duration'] = [ ['30days'], ['40days'] ]
import pandas as pd
technologies = {
'Courses': ["Spark", "PySpark", "Python", "pandas"],
'Fee': [20000, 25000, 22000, 30000],
'Duration': ['30days', '40days', '35days', '50days'],
'Discount': [1000, 2300, 1200, 2000]
}
df = pd.DataFrame(technologies)
print(df)
Yields below output.
Courses Fee Duration Discount 0 Spark 20000 30 days 1000 1 PySpark 25000 40 days 2300 2 Python 22000 35 days 1200 3 pandas 30000 50 days 2000
Courses Fee Duration Discount
0 Spark 20000 30 days 1000
1 PySpark 25000[30 days, 35 days] 2300
2 Python 22000 35 days 1200
3 pandas 30000 50 days 2000
Pandas.DataFrame.astype()
use to cast an object to a specified dtype. astype()
function also convert any suitable existing column to type. Post update, change the column type to object.
# Insert list into cell using df.astype(). df.at[1, 'Duration'] = [30, 35, 40] df['Duration'] = df['Duration'].astype('object') print(df)
pandas.DataFrame.insert ,pandas.DataFrame.info, pandas.DataFrame.isin , pandas.DataFrame.info
>>> df = pd.DataFrame({
'col1': [1, 2],
'col2': [3, 4]
}) >>>
df
col1 col2
0 1 3
1 2 4
>>>
df.insert(1, "newcol", [99, 99]) >>>
df
col1 newcol col2
0 1 99 3
1 2 99 4
>>>
df.insert(0, "col1", [100, 100], allow_duplicates = True) >>>
df
col1 col1 newcol col2
0 100 1 99 3
1 100 2 99 4
>>> df.insert(0, "col0", pd.Series([5, 6], index = [1, 2])) >>>
df
col0 col1 col1 newcol col2
0 NaN 100 1 99 3
1 5.0 100 2 99 4
Updated: August 3, 2019
import pandas as pd
df = pd.DataFrame({
'Region': ['West', 'North', 'South'],
'Company': ['Costco', 'Walmart', 'Home Depot'],
'Product': ['Dinner Set', 'Grocery', 'Gardening tools'],
'Month': ['September', 'July', 'February'],
'Sales': [2500, 3096, 8795]
})
df
data = [{
'Region': 'East',
'Company': 'Shop Rite',
'Product': 'Fruits',
'Month': 'December',
'Sales': 1265
}]
df.append(data, ignore_index = True, sort = False)
df.append(data, ignore_index = True, sort = True)
df.loc[3] = list(data[0].values()) or df.loc[len(df.index)] = list(data[0].values()) df
list(data[0].values()) = ['East', 'Shop Rite', 'Fruits', 'December', 1265]