简体   繁体   English

如何在插入连字符和空格的同时将多个Pandas系列类型的字符串连接到单个Pandas系列中?

[英]How to concatenate multiple Pandas series' of type string into a single Pandas series while inserting hyphens and spaces?

Problem: 问题:

I have some raw data with issues relating to the date and time information-- things like not having a colon to separate hours from minutes, as well as containing 2400 . 我有一些原始数据,涉及与日期和时间信息有关的问题-像没有冒号将小时和分钟分开,以及包含2400 I'm converting the individual columns to strings and modifying as required with the purpose of creating a single column of strings that can be parsed. 我正在将各个列转换为字符串,并根据需要进行修改,以创建可解析的单个字符串列。 I have about 20 data sets with about 35,000 rows each. 我有大约20个数据集,每个大约35,000行。

Sample Data: 样本数据:

a = ["2000"] * 100000
b = ["176"] * 100000
c = ["00:15","00:30","00:45","01:00"] * 25000   
d = {"year":a,"DOY":b,"time":c}
df = pd.DataFrame(d)

df.head()

    DOY time    year
0   176 00:15   2000
1   176 00:30   2000
2   176 00:45   2000
3   176 01:00   2000
4   176 00:15   2000

My slow solution: 我的慢速解决方案:

I have created the following line to complete the task but it is quite slow : 我创建了以下行以完成任务,但速度

df["date"] = [df["year"][i]+"-"+df["DOY"][i]+" "+df["time"][i] for i in range(0,len(df),1)]

df.head()

    DOY time    year    date
0   176 00:15   2000    2000-176 00:15
1   176 00:30   2000    2000-176 00:30
2   176 00:45   2000    2000-176 00:45
3   176 01:00   2000    2000-176 01:00
4   176 00:15   2000    2000-176 00:15

Question: 题:

What is the fastest way to concatenate the year , DOY , and time columns while inserting the appropriate hyphens and spaces for the purpose of parsing into datetime format? 插入 yearDOYtime列的同时插入适当的连字符和空格以将其解析为datetime格式的最快方法是什么? Or is this the wrong approach altogether? 还是这完全是错误的方法?

As always, thanks for advice. 一如既往,感谢您的建议。

This would be much faster than looping over the df . 这比循环df快得多。

df['Date'] = df['year'].map(str) + "-" + df['DOY'].map(str) + " " +  df['time'].map(str)

.map() maps the input values against a corresponding value in the passed in type, dict, Series, or function. .map()将输入值与传入的类型,字典,系列或函数中的对应值进行映射。 You can find more info in docs . 您可以在docs中找到更多信息。

More Info 更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM