﻿ Pandas: Create a Series of Timestamps from a DataFrame of integer or string columns - w3resource

# Pandas: Create a Series of Timestamps from a DataFrame of integer or string columns

## Pandas Time Series: Exercise-13 with Solution

Write a Pandas program to create a series of Timestamps from a DataFrame of integer or string columns. Also create a series of Timestamps using specified columns.

Sample Solution:

Python Code :

``````import pandas as pd
df = pd.DataFrame({'year': [2018, 2019, 2020],
'month': [2, 3, 4],
'day': [4, 5, 6],
'hour': [2, 3, 4]})
print("Original dataframe:")
print(df)
result = pd.to_datetime(df)
print("\nSeries of Timestamps from the said dataframe:")
print(result)
print("\nSeries of Timestamps using specified columns:")
print(pd.to_datetime(df[['year', 'month', 'day']]))
``````

Sample Output:

```Original dataframe:
year  month  day  hour
0  2018      2    4     2
1  2019      3    5     3
2  2020      4    6     4

Series of Timestamps from the said dataframe:
0   2018-02-04 02:00:00
1   2019-03-05 03:00:00
2   2020-04-06 04:00:00
dtype: datetime64[ns]

Series of Timestamps using specified columns:
0   2018-02-04
1   2019-03-05
2   2020-04-06
dtype: datetime64[ns]
```

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz

﻿

## Python: Tips of the Day

Merging two dicts in Python 3.5+ with a single expression

Example:

```# How to merge two dictionaries
# in Python 3.5+

x = {'p': 1, 'q': 3}
y = {'q': 5, 'r': 8}

z = {**x, **y}

z
{'r': 4, 'p': 1, 'q': 3}

z = dict(x, **y)
print(z)
```

Output:

```{'p': 1, 'q': 5, 'r': 8}
```