【问题标题】:Retrieving a specific value from a column and store it in a new column depending on the conditions that has been set根据已设置的条件从列中检索特定值并将其存储在新列中
【发布时间】:2022-11-13 23:36:02
【问题描述】:

我是熊猫新手,我需要帮助。我有一组给定的数据:

Index sensor timestamp
0 temperature 10/09/2019 10:49:00
1 humidity 10/09/2019 10:50:00
2 light 10/09/2019 10:50:00
3 motion 10/09/2019 10:50:00
4 temperature 10/09/2019 11:19:00
5 humidity 10/09/2019 11:20:00
6 light 10/09/2019 11:20:00
7 motion 10/09/2019 11:20:00
8 temperature 10/09/2019 11:34:00

给定的数据对我来说不是很系统,因此我想添加一个名为 temperature 的新列并存储其对应的 timestamp 值。

我想创建一个名为Temperature 的新列并存储它对应的时间戳值。预期的数据框将如下图所示:

index sensor timestamp temperature
0 temperature 10/09/2019 10:49:00 10/09/2019 10:49:00
1 humidity 10/09/2019 10:50:00 not related
2 light 10/09/2019 10:50:00 not related
3 motion 10/09/2019 10:50:00 not related
4 temperature 10/09/2019 11:19:00 10/09/2019 11:19:00
5 humidity 10/09/2019 11:20:00 not related
6 light 10/09/2019 11:20:00 not related
7 motion 10/09/2019 11:20:00 not related
8 temperature 10/09/2019 11:34:00 10/09/2019 11:34:00

我提出的想法是检查sensor 列中的每一行是否包含temperature。我创建了一个空列表,以便稍后可以附加该值并将其添加到原始数据框中。

List = []

如果sensor = 'temperature' 则时间戳值将存储在新列中,并且在sensor != 'temperature' 时给出'not_related'。我试图将这个想法转换为代码,这就是我卡住的地方。

for row in df['sensor']:
    if row == 'temperature' : List.append(df.loc[df[df['sensor']=='temperature'].index.values , 'timestamp'])
    else : List.append('Not related')

代码的问题在于它存储全部时间戳值等于temperature 而不是其对应的单个值。

运行这些代码时得到的示例:

List[4] 
0       2019-10-09 10:49:00
4       2019-10-09 11:19:00
8       2019-10-09 11:34:00
12      2019-10-09 11:49:00
16      2019-10-09 12:04:00
                ...        
86703   2021-03-22 13:29:00
86898   2021-03-25 14:36:00
86903   2021-03-25 14:51:00
86944   2021-03-28 16:52:00
87325   2021-07-19 10:03:00
Name: timestamp, Length: 8236, dtype: datetime64[ns]
List[1] 

'Not related'

List[0:5] 
[0       2019-10-09 10:49:00
 4       2019-10-09 11:19:00
 8       2019-10-09 11:34:00
 12      2019-10-09 11:49:00
 16      2019-10-09 12:04:00
                 ...        
 86703   2021-03-22 13:29:00
 86898   2021-03-25 14:36:00
 86903   2021-03-25 14:51:00
 86944   2021-03-28 16:52:00
 87325   2021-07-19 10:03:00
 Name: timestamp, Length: 8236, dtype: datetime64[ns],
 'Not related',
 'Not related',
 'Not related',
 0       2019-10-09 10:49:00
 4       2019-10-09 11:19:00
 8       2019-10-09 11:34:00
 12      2019-10-09 11:49:00
 16      2019-10-09 12:04:00
                 ...        
 86703   2021-03-22 13:29:00
 86898   2021-03-25 14:36:00
 86903   2021-03-25 14:51:00
 86944   2021-03-28 16:52:00
 87325   2021-07-19 10:03:00
 Name: timestamp, Length: 8236, dtype: datetime64[ns]]

之所以有这样的想法,是为了方便我以后在列之间进行计算。任何见解或其他方法将不胜感激。

【问题讨论】:

  • 请阐明您的具体问题或提供更多详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。

标签: python pandas dataframe


【解决方案1】:

您可以使用 np.where() 来给出条件值。因此,例如您可以使用如下命令来表示,如果 df['sensor'] == 'temperature',则从 df['timestamp'] 中获取相应的值。如果不是,则将该值设置为“不相关”。

这应该有效:

df['temperature'] = np.where(df['sensor'] == 'temperature', df['timestamp'], 'not related')

【讨论】:

    猜你喜欢
    • 2021-05-13
    • 2023-01-13
    • 1970-01-01
    • 2011-07-17
    • 1970-01-01
    • 2018-08-24
    • 2015-05-30
    • 2020-06-13
    • 1970-01-01
    相关资源
    最近更新 更多