熊猫根据条件创建单独的DF

时间:2020-02-10 17:15:29

标签: python pandas

我有一些看起来像这样的数据:

import cv2
import os

output_dir = "/home/Desktop/stream_rec"

# Load Streams
input_cap = {}
output_cap = {}

fourcc = cv2.VideoWriter_fourcc(*'XVID')

for i in range(2, 6):
    input_cap[i] = cv2.VideoCapture("rtsp://stream@10.0.0.{}".format(i))
    output_cap[i] = cv2.VideoWriter(os.path.join(output_dir, f'D{i}.mp4'), fourcc, 25.0, (1080,1920)[::-1])


# Load Frames
# while all([stream.isOpened() for stream_id, stream in streams.items()]):
while True:

    for stream_id, stream in input_cap.items():

        # Read
        ret, frame = stream.read()

        # Write
        output_cap[stream_id].write(frame)

        # Show
        # frame = cv2.resize(frame, (int(frame.shape[1]/2), int(frame.shape[0]/ 2)))
        if ret:
            cv2.imshow(str(stream_id), frame)
        else:
            break

    if cv2.waitKey(1) & 0xFF == ord('q'): # Press Q to stop recording
        break

# Release Caps
for i in range(2, 6):
    input_cap[i].release()
    output_cap[i].release()

如何根据 kW OAT month hour Day_of_week Date 2018-02-18 10:45:00 44.0 63.9 2 10 6 2018-02-18 10:50:00 48.0 63.8 2 10 6 2018-02-18 10:55:00 48.0 63.8 2 10 6 2018-02-18 11:00:00 48.0 63.5 2 11 6 2018-02-18 11:05:00 54.0 63.2 2 11 6 列== 6,7,8或9来创建名为summer的其他熊猫数据框?

此尝试引发ValueError:值的长度与索引的长度不匹配

month

我希望有一些简单的东西: df['summer'] = [1 if 'month' == 6 else 0]

我完全在正确的道路上!谢谢!

1 个答案:

答案 0 :(得分:1)

如果您要创建一个单独的数据框“夏季”,则可以尝试以下代码-

summer = df[df['month'].isin([6,7,8,9])].reset_index(drop=True)

如果要在现有数据框“ df”中添加名为“ summer”的列,则可以尝试以下操作-

import numpy as np  
df['summer'] = np.where(df['month'].isin([6,7,8,9]), 1, 0)