'how to find last row in particular column in from excel using python pandas or openpyxl [duplicate]
In My excel other columns having values if column 'A' will have values till 8 row means it will not be the same when we refer other columns 'D' OR 'E' I want to find last row of particular column. Because I need to use the number in for loop range. Kindly help
i found many answers but all are calculating if any column contains value also it will give that number. I used below
last_empty_row=len(sheet_obj['G'])+1
but it gives same result as
row=sheet_obj.max_row
This is not a duplicate question.
I do not know how it will be below link accepted answer for my question.
How to find the last row in a column using openpyxl normal workbook?
ws.max_row len(ws['A'])
will give same answer this is not the answer i am loooking for. What i am looking for is below accepted answer is correct one find last row in particular column.
Solution 1:[1]
Perhaps this works:
df['G'].notna().sum()
Example:
import pandas as pd
df = pd.read_excel('test.xlsx')
df['A'].notna().sum()
result: 3
df['B'].notna().sum()
result: 1
Solution 2:[2]
data = {'Name':['Tom', 'nick', 'krish', 'jack'], 'Age':[20, 21, 19, 18]} #you can create a dataframe or import an excel file by using pd.read_excel('filename.xlsx')
df=pd.DataFrame(data)
df['Age'].tail(1) #to find the last value of column Age
Output: 18
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Ranjan |

