将列分配给pandas df

用户9394674

我正在尝试将分配Column给现有df具体来说,某些时间戳会被排序,但当前导出是单独的series我想将此附加到df

import pandas as pd

d = ({           
    'time' : ['08:00:00 am','12:00:00 pm','16:00:00 pm','20:00:00 pm','2:00:00 am','13:00:00 pm','3:00:00 am'], 
    'code' : ['A','B','C','A','B','C','A'], 
    })

df = pd.DataFrame(data=d)

df['time'] = pd.to_timedelta(df['time'])

cutoff, day = pd.to_timedelta(['3.5H', '24H'])
df.time.apply(lambda x: x if x > cutoff else x + day).sort_values().reset_index(drop=True)
x = df.time.apply(lambda x: x if x > cutoff else x + day).sort_values().reset_index(drop=True).dt.components
x = x.apply(lambda x: '{:02d}:{:02d}:{:02d}'.format(x.days*24+x.hours, x.minutes, x.seconds), axis=1)

输出:

0    08:00:00
1    12:00:00
2    13:00:00
3    16:00:00
4    20:00:00
5    26:00:00
6    27:00:00

我改变了

df['time'] = x.apply(lambda x: '{:02d}:{:02d}:{:02d}'.format(x.days*24+x.hours, x.minutes, x.seconds), axis=1)

但这产生了

       time code
0  08:00:00    A
1  12:00:00    B
2  13:00:00    C
3  16:00:00    A
4  20:00:00    B
5  26:00:00    C
6  27:00:00    A

如你看到的。排序后,时间戳未与其各自的值对齐。

预期的输出是:

       time code
0  08:00:00    A
1  12:00:00    B
2  13:00:00    C
3  16:00:00    C
4  20:00:00    A
5  26:00:00    B
6  27:00:00    A
神圣

从您的代码中删除reset_index(drop = True),稍后排序可能对您有用。

import pandas as pd

d = ({           
    'time' : ['08:00:00 am','12:00:00 pm','16:00:00 pm','20:00:00 pm','2:00:00 am','13:00:00 pm','3:00:00 am'], 
    'code' : ['A','B','C','A','B','C','A'], 
    })

df = pd.DataFrame(data=d)

df['time'] = pd.to_timedelta(df['time'])

cutoff, day = pd.to_timedelta(['3.5H', '24H'])

x = df.time.apply(lambda x: x if x > cutoff else x + day).dt.components
df['time'] = x.apply(lambda x: '{:02d}:{:02d}:{:02d}'.format(x.days*24+x.hours, x.minutes, x.seconds), axis=1)
df = df.sort_values('time')

print(df)

熊猫通过索引进行对齐。reset_index(drop = True)破坏了原始索引,并导致已排序的时间列被顺序分配回去。这可能就是为什么您没有得到什么。

原始时间栏。

0   08:00:00
1   12:00:00
2   16:00:00
3   20:00:00
4   02:00:00
5   13:00:00
6   03:00:00

在sort_values()之后。

4   02:00:00
6   03:00:00
0   08:00:00
1   12:00:00
5   13:00:00
2   16:00:00
3   20:00:00

在reset_index(drop = True)之后

0   02:00:00
1   03:00:00
2   08:00:00
3   12:00:00
4   13:00:00
5   16:00:00
6   20:00:00

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将列表值分配给 Pandas df 列会生成 NaN 或长度错误

将列表分配给 df 的子集

将值分配给另一个df的df $列?

使用第二个熊猫df中的条件将值分配给列

使用np.where有条件地将值分配给DF列

熊猫:如何在函数内将sum()或mean()分配给df.groupby?

range(1:len(df))将NaN分配给数据帧中的最后一行

如何将列名称从源数据帧分配给目标df

将 df 中的数据分配给 Python 中带有循环的列表

将列名分配给 df,阻止字符自动转换为数字?

根据日期将新变量(来自不同的df)分配给数据框

如何基于概率分布将从列表中选择的值分配给df列?

需要将 df.apply 的结果分配给两个新列

如何防止熊猫仅将一个df的值分配给另一行的另一列?

无法将值分配给多索引数据帧中的单元格(分配给df的副本/切片?)

Python,有没有一种方法可以将df.drop分配给新变量?

将值分配给Pandas中的多个列

将 numpy 矩阵分配给 pandas 列

无法将值分配给Pandas中的某些列

如何选择NaN行并将value_A分配给新列?代码df.loc ['condition_1'&'condition_2'&'df.column_a ==''] ='value_a'

在等于值的地方分配列 - pandas df

Pandas:df['A'] == df['B'] 或 df['B'] == [] 的行数

Python-根据文本中出现的字符串将一个熊猫df的值分配给另一个

在R中使用ifelse()将变量分配给tbl_df / data.frame对象会导致R的内存不足

将NumericVector分配给DataFrame的列

将列内容分配给类别

将pandas df排序为单独的列

如何将pandas.core.series.Series分配给pandas数据框列

将值分配给按索引和列过滤的pandas数据框列