【发布时间】:2021-09-29 14:57:59
【问题描述】:
我有这个数据集,格式如下:
time_col val_col value
0 2021-04-28 12:10:00 10 Students
1 2021-04-28 12:20:00 20 Students
2 2021-04-28 12:30:00 20 Students
3 2021-04-28 12:40:00 35 Students
4 2021-04-28 12:50:00 35 Students
5 2021-04-28 12:10:00 100 Noise
6 2021-04-28 12:20:00 130 Noise
7 2021-04-28 12:30:00 155 Noise
8 2021-04-28 12:40:00 160 Noise
9 2021-04-28 12:50:00 175 Noise
时间是重复的,所以我希望输出看起来像:
time_col Students Noise
0 2021-04-28 12:10:00 10 100
1 2021-04-28 12:20:00 20 130
2 2021-04-28 12:30:00 20 155
3 2021-04-28 12:40:00 35 160
4 2021-04-28 12:50:00 35 175
请告诉我怎么做。
这是我制作第一个 df 的方式:
time_col = [np.datetime64('2021-04-28T12:10:00'), np.datetime64('2021-04-28T12:20:00'), np.datetime64('2021-04-28T12:30:00'), np.datetime64('2021-04-28T12:40:00'), np.datetime64('2021-04-28T12:50:00'),
np.datetime64('2021-04-28T12:10:00'), np.datetime64('2021-04-28T12:30:00'), np.datetime64('2021-04-28T12:30:00'), np.datetime64('2021-04-28T12:40:00'), np.datetime64('2021-04-28T12:50:00')]
val_col = [10,20,20,35,35, 100,135,155,160,175]
value = ["Students", "Students", "Students", "Students", "Students", "Noise", "Noise", "Noise", "Noise", "Noise"]
df_1 = pd.DataFrame([time_col, val_col, value])
df_2 = pd.DataFrame.transpose(df_1)
df_2.columns = ["time_col", "val_col", "value"]
如您所见,我是 python 新手,我知道这不是创建 df 的最佳方式
【问题讨论】:
-
欢迎来到 Stack Overflow!请使用tour,阅读what's on-topic here、How to Ask 和question checklist,并提供minimal reproducible example 以显示您的具体问题。 “为我实现此功能”与此站点无关,因为 SO 不是免费的在线编码服务。你必须诚实地尝试,然后就你的算法或技术提出一个具体问题。
标签: python pandas dataframe numpy