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.
data = json.loads(requests.get('https://tradestie.com/api/v1/apps/ttm-squeeze-stocks?date=2022-11-17').text)
data[0]
{'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'}
df = pd.DataFrame(data)
Let us look at our data.
df.head(1)
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.
df10rows = df.head(10)
len(df10rows)
10
df10rows.head(2)
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.
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.
dfrow11 = pd.DataFrame([row11])
dfrow11
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 |
pd.concat([df10rows,pd.DataFrame([row11])])
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.
dfrow11.index = range(10,11)
dfrow11
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.
pd.concat([df10rows,dfrow11])
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.
df10rows.combine_first(dfrow11).head(11)
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