[英]How to count rows in a data frame in Pandas conditionally against values in another column of the same data frame?
我有一個數據框,其中有行,我想有條件地計數
TIME VALUE Prev_Time
0 23:01 0 NaN
1 23:02 0 NaN
2 23:03 1 23:02
3 23:04 0 NaN
4 23:05 0 NaN
5 23:06 1 23:05
6 23:07 0 NaN
7 23:08 0 NaN
8 23:09 0 NaN
9 23:10 0 NaN
10 23:11 1 23:10
11 23:12 0 NaN
12 23:13 0 NaN
13 23:14 0 NaN
14 23:15 0 NaN
15 23:16 1 23:15
16 23:17 0 NaN
我想根據“ Prev_Time”列上的條件對行進行計數,以便...
所需的輸出應為
ROW_COUNT
0 2
1 3
2 5
3 5
4 2
我也想要總計數,像(len(df))這樣的東西,應該打印出來
Total Count: 5
找到好的台詞:
notnull=df[df.VALUE>0]
"""
TIME VALUE Prev_Time
2 23:03 1 23:02
5 23:06 1 23:05
10 23:11 1 23:10
15 23:16 1 23:15
"""
使用np.split
中斷:
row_counts=pd.DataFrame({'ROW_COUNT':[len(x) for x in np.split(df,notnull.index)]})
"""
ROW_COUNT
0 2
1 3
2 5
3 5
4 2
"""
並計數:
len(row_counts)
"""
5
"""
這有點奏效,您可以根據需要調整代碼,但是有些基本概念!
#Dummy data set
df1 = pd.DataFrame({'TIME': np.arange(17), 'VALUE': np.arange(-17,0), 'Prev_time': [np.nan, np.nan,1, np.nan, np.nan,2, np.nan, np.nan, np.nan, np.nan,4, np.nan, np.nan, np.nan, np.nan,5, np.nan]})
#gets the rows that are not null and extracts their index number
df=df1[df1['Prev_time'].notnull()].reset_index()
#Checking for the case where the last row might be null,
#need to add it manually to the index
if df.loc[len(df)-1]['index'] != (len(df1)-1):
df.loc[len(df)]=[len(df1),0,0,0]
count=df['index']-df['index'].shift(1).fillna(0)
len(count)
這可能不是一個完美的答案,它將滿足您的要求:
import pandas as pd
#read the data
d = pd.read_csv('stackdata.txt')
#we need the last row to be identified, so give it a value
d['Prev_Time'][len(d)-1]=1
#get all the rows where Prev_Time is not null
ds = d[d.Prev_Time.notnull()]
#reset the index, you shall get an additional column named index
ds = ds.reset_index()
#get only the newly added index column
dst = ds[ds.columns[0]]
#get the diff of the series
dstr = dst.diff()
#Get the first value from the previous series and assign it.
dstr[0] = dst[0]
#Addd +1 to the last item -- result required.
dstr[len(dstr)-1] +=1
len(dstr)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.