NbShare
  • Nbshare Notebooks

  • Table of Contents

  • Python Utilities

    • How To Install Jupyter Notebook
    • How to Upgrade Python Pip
    • How To Use Python Pip
  • Python

    • Python Datetime
    • Python Dictionary
    • Python Generators
    • Python Iterators and Generators
    • Python Lambda
    • Python Sort List
    • String And Literal In Python 3
    • Strftime and Strptime In Python
    • Python Tkinter
    • Python Underscore
    • Python Yield
  • Pandas

    • Aggregating and Grouping
    • DataFrame to CSV
    • DF to Numpy Array
    • Drop Columns of DF
    • Handle Json Data
    • Iterate Over Rows of DataFrame
    • Merge and Join DataFrame
    • Pivot Tables
    • Python List to DataFrame
    • Rename Columns of DataFrame
    • Select Rows and Columns Using iloc, loc and ix
    • Sort DataFrame
  • PySpark

    • Data Analysis With Pyspark
    • Read CSV
    • RDD Basics
  • Data Science

    • Confusion Matrix
    • Decision Tree Regression
    • Logistic Regression
    • Regularization Techniques
    • SVM Sklearn
    • Time Series Analysis Using ARIMA
  • Machine Learning

    • How To Code RNN and LSTM Neural Networks in Python
    • PyTorch Beginner Tutorial Tensors
    • Rectified Linear Unit For Artificial Neural Networks Part 1 Regression
    • Stock Sentiment Analysis Using Autoencoders
  • Natural Language
    Processing

    • Opinion Mining Aspect Level Sentiment Analysis
    • Sentiment Analysis using Autoencoders
    • Understanding Autoencoders With Examples
    • Word Embeddings Transformers In SVM Classifier
  • R

    • DataFrame to CSV
    • How to Create DataFrame in R
    • How To Use Grep In R
    • How To Use R Dplyr Package
    • Introduction To R DataFrames
    • Tidy Data In R
NbShare Notebooks
  • Publish Your Post On nbshare.io

  • R Python Pandas Data Science Excel NLP Numpy Pyspark Finance

How to append rows with concat to a Pandas DataFrame

In [19]:
import pandas as pd
import requests
import json

For this example, I will load the data from the following api. This is stocks data. Let us not worry about the technical aspect of this data.

In [20]:
data = json.loads(requests.get('https://tradestie.com/api/v1/apps/ttm-squeeze-stocks?date=2022-11-17').text)
In [21]:
data[0]
Out[21]:
{'date': 'Thu, 17 Nov 2022 00:00:00 GMT',
 'in_squeeze': False,
 'no_of_days_in_squeeze': 0,
 'no_of_days_out_of_squeeze': 1,
 'out_of_squeeze': True,
 'ticker': 'AA'}
In [22]:
df = pd.DataFrame(data)

Let us look at our data.

In [23]:
df.head(1)
Out[23]:
date in_squeeze no_of_days_in_squeeze no_of_days_out_of_squeeze out_of_squeeze ticker
0 Thu, 17 Nov 2022 00:00:00 GMT False 0 1 True AA

This data is huge. But for our purpose, I will just slice the first 10 rows of this dataframe.

In [24]:
df10rows = df.head(10)
In [25]:
len(df10rows)
Out[25]:
10
In [26]:
df10rows.head(2)
Out[26]:
date in_squeeze no_of_days_in_squeeze no_of_days_out_of_squeeze out_of_squeeze ticker
0 Thu, 17 Nov 2022 00:00:00 GMT False 0 1 True AA
1 Thu, 17 Nov 2022 00:00:00 GMT True 0 0 False AADI

As we know, there are 10 rows in this data. Let us now append another row (row11) to this dataframe.

In [27]:
row11 = {'date': 'Thu, 17 Nov 2022 00:00:00 GMT',
 'in_squeeze': False,
 'no_of_days_in_squeeze': 0,
 'no_of_days_out_of_squeeze': 1,
 'out_of_squeeze': True,
 'ticker': 'INTC'}

First we need to convert this data in to a dataframe.

In [28]:
dfrow11 = pd.DataFrame([row11])
In [29]:
dfrow11
Out[29]:
date in_squeeze no_of_days_in_squeeze no_of_days_out_of_squeeze out_of_squeeze ticker
0 Thu, 17 Nov 2022 00:00:00 GMT False 0 1 True INTC

Concat row to a Pandas Dataframe

In [30]:
pd.concat([df10rows,pd.DataFrame([row11])])
Out[30]:
date in_squeeze no_of_days_in_squeeze no_of_days_out_of_squeeze out_of_squeeze ticker
0 Thu, 17 Nov 2022 00:00:00 GMT False 0 1 True AA
1 Thu, 17 Nov 2022 00:00:00 GMT True 0 0 False AADI
2 Thu, 17 Nov 2022 00:00:00 GMT True 0 0 False AAL
3 Thu, 17 Nov 2022 00:00:00 GMT True 1 0 False AAME
4 Thu, 17 Nov 2022 00:00:00 GMT False 0 1 True AATC
5 Thu, 17 Nov 2022 00:00:00 GMT False 0 1 True AAU
6 Thu, 17 Nov 2022 00:00:00 GMT True 1 0 False AAWW
7 Thu, 17 Nov 2022 00:00:00 GMT False 0 1 True ABCB
8 Thu, 17 Nov 2022 00:00:00 GMT True 1 0 False ABEO
9 Thu, 17 Nov 2022 00:00:00 GMT False 0 1 True ABIO
0 Thu, 17 Nov 2022 00:00:00 GMT False 0 1 True INTC

As we see above the row is appended to the last. Note the index of the last row is same as it is of first row.

We can fix it using pd.index command.

In [31]:
dfrow11.index = range(10,11)
In [32]:
dfrow11
Out[32]:
date in_squeeze no_of_days_in_squeeze no_of_days_out_of_squeeze out_of_squeeze ticker
10 Thu, 17 Nov 2022 00:00:00 GMT False 0 1 True INTC

Let us try to append again.

In [33]:
pd.concat([df10rows,dfrow11])
Out[33]:
date in_squeeze no_of_days_in_squeeze no_of_days_out_of_squeeze out_of_squeeze ticker
0 Thu, 17 Nov 2022 00:00:00 GMT False 0 1 True AA
1 Thu, 17 Nov 2022 00:00:00 GMT True 0 0 False AADI
2 Thu, 17 Nov 2022 00:00:00 GMT True 0 0 False AAL
3 Thu, 17 Nov 2022 00:00:00 GMT True 1 0 False AAME
4 Thu, 17 Nov 2022 00:00:00 GMT False 0 1 True AATC
5 Thu, 17 Nov 2022 00:00:00 GMT False 0 1 True AAU
6 Thu, 17 Nov 2022 00:00:00 GMT True 1 0 False AAWW
7 Thu, 17 Nov 2022 00:00:00 GMT False 0 1 True ABCB
8 Thu, 17 Nov 2022 00:00:00 GMT True 1 0 False ABEO
9 Thu, 17 Nov 2022 00:00:00 GMT False 0 1 True ABIO
10 Thu, 17 Nov 2022 00:00:00 GMT False 0 1 True INTC

As we the index of last row is 10 now.

If the index is unique, we can also use pd.combine_first method of pandas as shown below.

In [34]:
df10rows.combine_first(dfrow11).head(11)
Out[34]:
date in_squeeze no_of_days_in_squeeze no_of_days_out_of_squeeze out_of_squeeze ticker
0 Thu, 17 Nov 2022 00:00:00 GMT False 0 1 True AA
1 Thu, 17 Nov 2022 00:00:00 GMT True 0 0 False AADI
2 Thu, 17 Nov 2022 00:00:00 GMT True 0 0 False AAL
3 Thu, 17 Nov 2022 00:00:00 GMT True 1 0 False AAME
4 Thu, 17 Nov 2022 00:00:00 GMT False 0 1 True AATC
5 Thu, 17 Nov 2022 00:00:00 GMT False 0 1 True AAU
6 Thu, 17 Nov 2022 00:00:00 GMT True 1 0 False AAWW
7 Thu, 17 Nov 2022 00:00:00 GMT False 0 1 True ABCB
8 Thu, 17 Nov 2022 00:00:00 GMT True 1 0 False ABEO
9 Thu, 17 Nov 2022 00:00:00 GMT False 0 1 True ABIO
10 Thu, 17 Nov 2022 00:00:00 GMT False 0 1 True INTC

Related Notebooks

  • How To Iterate Over Rows In A Dataframe In Pandas
  • How to Export Pandas DataFrame to a CSV File
  • Pandas How To Sort Columns And Rows
  • How to Convert Python Pandas DataFrame into a List
  • How to Sort Pandas DataFrame with Examples
  • How To Convert Python List To Pandas DataFrame
  • How to Plot a Histogram in Python
  • Append In Python
  • How To Replace na Values with Zeros In R Dataframe

Register

User Already registered.


Login

Login

We didn't find you! Please Register

Wrong Password!


Register
    Top Notebooks:
  • Data Analysis With Pyspark Dataframe
  • Strftime and Strptime In Python
  • Python If Not
  • Python Is Integer
  • Dictionaries in Python
  • How To install Python3.9 With Conda
  • String And Literal In Python 3
  • Privacy Policy
©