'Finding the last position of each column in data frame by Pandas
I have done some searching to answer this question, but all I can not figure out how to do this:
I have a dataset which contains 185 rows and 30 columns. Not all of the rows have value. I want to look for the position of the last value on each column and take the index of that column. I am not sure how to do this operation as when I use the code below it gives me the length of the data frame not just that column :
len(data_exam['col'])
I would appreciate for any suggestion.
Also I want to make sure that if I want to read all the columns in a loop, does my following code would be a good choice or not! :
list=[]
for col in data:
function which find the length of column
Thanks.
Solution 1:[1]
IIUC, you want the value of the last non-nan in each column:
df[::-1].bfill().iloc[0]
Example:
df = pd.DataFrame({'A':[1,2,3,4,np.nan],'B':[1,np.nan,np.nan,np.nan,np.nan],'C':[1,2,3,4,5]})
A B C
0 1.0 1.0 1
1 2.0 NaN 2
2 3.0 NaN 3
3 4.0 NaN 4
4 NaN NaN 5
Output:
A 4.0
B 1.0
C 5.0
Name: 4, dtype: float64
Solution 2:[2]
df.reset_index().melt('index').dropna().groupby('variable')['index'].max()
Out[487]:
variable
A 3
B 0
C 4
Name: index, dtype: int64
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 | Scott Boston |
| Solution 2 | BENY |
