因此,我有一台可以为我提供人流计数(进出)的设备。它根据绘制的线条数生成一个csv。 csv格式如下:
timestamp, in, out
以上情况是因为我只有1行。但是,使用这种格式,我每行要进/出数个
timestamp, in, out, in, out, in, out, in, out
输入示例:
12/01/2020,16:02:00,0,0,0,2,0,0,0,0
12/01/2020,16:03:00,0,0,0,0,0,0,0,0
12/01/2020,16:04:00,0,0,0,0,0,0,0,0
12/01/2020,16:05:00,0,0,0,0,0,0,0,0
12/01/2020,17:06:00,0,0,0,0,0,0,0,0
12/01/2020,17:07:06,1,0,0,0,0,0,0,0
12/01/2020,17:08:00,0,0,0,0,0,0,0,0
12/01/2020,17:09:01,0,0,0,0,0,0,0,1
12/01/2020,18:10:00,0,0,0,0,0,0,0,0
12/01/2020,18:11:00,1,0,0,0,0,0,0,0
我希望每小时计算in
和out
的总和。
结果应采用以下格式:
timestamp, ins, outs
答案 0 :(得分:0)
在阅读my_csv_file.csv
之后,您应该添加相应的in / out列,创建一个timestamp列,并按小时级别将时间戳分组:
import pandas as pd
# Read file, no header!
df = pd.read_csv('my_csv_file.csv', header=None)
n_cols = len(df.columns)
# Sum all inputs and outputs
df['in'] = df.iloc[:,range(2,n_cols ,2)].sum(axis=1)
df['out'] = df.iloc[:,range(3,n_cols ,2)].sum(axis=1)
df = df.drop(columns=range(2,n_cols))
# Create a timestamp with the date and hour
df['timestamp'] = pd.to_datetime((df[0] + ' ' + df[1]))
df =df.drop(columns=[0,1])
# Groupby same hour and same date and sum
df_grouped = df.groupby([df.timestamp.dt.date, df.timestamp.dt.hour], group_keys=False).sum()
# Prettify the output
df_grouped.index.names = ['date', 'hour']
df_grouped = df_grouped.reset_index()
# date hour in out
#0 2020-12-01 16 0 2
#1 2020-12-01 17 1 1
#2 2020-12-01 18 1 0
注意:要重新创建本例中使用的数据,可以使用以下代码行(代替read_csv
)
df = pd.DataFrame({0: {0: '12/01/2020', 1: '12/01/2020', 2: '12/01/2020', 3: '12/01/2020', 4: '12/01/2020', 5: '12/01/2020', 6: '12/01/2020', 7: '12/01/2020', 8: '12/01/2020', 9: '12/01/2020'}, 1: {0: '16:02:00', 1: '16:03:00', 2: '16:04:00', 3: '16:05:00', 4: '17:06:00', 5: '17:07:06', 6: '17:08:00', 7: '17:09:01', 8: '18:10:00', 9: '18:11:00'}, 2: {0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 1, 6: 0, 7: 0, 8: 0, 9: 1}, 3: {0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0}, 4: {0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0}, 5: {0: 2, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0}, 6: {0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0}, 7: {0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0}, 8: {0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0}, 9: {0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 1, 8: 0, 9: 0}})
答案 1 :(得分:0)
请参阅以下代码。每行的说明均已注释。
df=pd.read_csv(path_here,sep=",",header=None)
df=df.rename(columns={0:"date",1:"timestamp"})
#Get all headers that are not timestamp and date
headers=list(df.columns)
headers.remove("timestamp")
headers.remove("date")
# Unpivot data so each value is in single record
df=df.melt(id_vars=["date","timestamp"],value_vars=headers,var_name="type",value_name="value")
# Change data type for aggregation (even is in and odd is out)
df["type"]=df["type"].apply(lambda x: "in" if x%2==0 else "out")
# group by timestamp,type and find the sum of value
df=df.groupby(["date","timestamp","type"],as_index=False)["value"].sum()
# pivot table to get in and out of single time stamp in a record
df=df.pivot_table(index=["date","timestamp"],columns="type",values="value")
df=df.reset_index()