Python pandas Excel

Peter Otten __peter__ at web.de
Sat Jul 18 04:20:56 EDT 2020


J Conrado wrote:

> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> HI,
> 
> 
> I have an excel file with several columns, the first day/month,/year and
> hour:
> 
> 
> Data
> 01/11/2017 00:00
> 01/11/2017 03:00
> 01/11/2017 06:00
> 01/11/2017 09:00
> 01/11/2017 12:00
> 01/11/2017 15:00
> 01/11/2017 18:00
> 01/11/2017 21:00
> 02/11/2017 00:00
> 02/11/2017 03:00
> 02/11/2017 06:00
> 02/11/2017 09:00
> 02/11/2017 12:00
> 02/11/2017 15:00
> 02/11/2017 18:00
> 02/11/2017 21:00
> 03/11/2017 00:00
> 03/11/2017 03:00
> 03/11/2017 06:00
> 03/11/2017 09:00
> 03/11/2017 12:00
> 03/11/2017 15:00
> 03/11/2017 18:00
> 03/11/2017 21:00
> 04/11/2017 00:00
> 04/11/2017 03:00
> 04/11/2017 06:00
> 04/11/2017 09:00
> 04/11/2017 12:00
> 04/11/2017 15:00
> 04/11/2017 18:00
> 04/11/2017 21:00
> 05/11/2017 00:00
> 05/11/2017 03:00
> 05/11/2017 06:00
> 05/11/2017 09:00
> 05/11/2017 12:00
> 05/11/2017 15:00
> 05/11/2017 18:00
> 05/11/2017 21:00
> 06/11/2017 00:00
> 06/11/2017 03:00
> 06/11/2017 06:00
> 06/11/2017 09:00
> 06/11/2017 12:00
> 06/11/2017 15:00
> 06/11/2017 18:00
> 06/11/2017 21:00
> 07/11/2017 00:00
> 07/11/2017 03:00
> 07/11/2017 06:00
> 07/11/2017 09:00
> 07/11/2017 12:00
> 07/11/2017 15:00
> 07/11/2017 18:00
> 07/11/2017 21:00
> 08/11/2017 00:00
> 08/11/2017 03:00
> 08/11/2017 06:00
> 08/11/2017 09:00
> 08/11/2017 12:00
> 08/11/2017 15:00
> 08/11/2017 21:00
> 09/11/2017 00:00
> 09/11/2017 03:00
> 09/11/2017 06:00
> 09/11/2017 09:00
> 09/11/2017 12:00
> 09/11/2017 15:00
> 09/11/2017 18:00
> 09/11/2017 21:00
> 10/11/2017 00:00
> 10/11/2017 03:00
> 10/11/2017 06:00
> 10/11/2017 09:00
> 10/11/2017 12:00
> 10/11/2017 15:00
> 10/11/2017 18:00
> 10/11/2017 21:00
> 11/11/2017 00:00
> 11/11/2017 03:00
> 11/11/2017 06:00
> 11/11/2017 09:00
> 11/11/2017 12:00
> 11/11/2017 15:00
> 11/11/2017 18:00
> 11/11/2017 21:00
> 12/11/2017 00:00
> 12/11/2017 03:00
> 12/11/2017 06:00
> 12/11/2017 09:00
> 12/11/2017 12:00
> 12/11/2017 15:00
> 12/11/2017 18:00
> 12/11/2017 21:00
> 13/11/2017 00:00
> 13/11/2017 03:00
> 13/11/2017 06:00
> 13/11/2017 09:00
> 13/11/2017 12:00
> 13/11/2017 15:00
> 13/11/2017 18:00
> 13/11/2017 21:00
> 14/11/2017 00:00
> 14/11/2017 03:00
> 14/11/2017 06:00
> 14/11/2017 09:00
> 14/11/2017 12:00
> 14/11/2017 15:00
> 14/11/2017 18:00
> 14/11/2017 21:00
> 15/11/2017 00:00
> 15/11/2017 03:00
> 15/11/2017 06:00
> 15/11/2017 09:00
> 15/11/2017 12:00
> 15/11/2017 15:00
> 15/11/2017 18:00
> 15/11/2017 21:00
> 16/11/2017 00:00
> 16/11/2017 03:00
> 16/11/2017 06:00
> 16/11/2017 09:00
> 16/11/2017 12:00
> 16/11/2017 15:00
> 16/11/2017 18:00
> 16/11/2017 21:00
> 17/11/2017 00:00
> 17/11/2017 03:00
> 17/11/2017 06:00
> 17/11/2017 09:00
> 17/11/2017 12:00
> 17/11/2017 15:00
> 17/11/2017 18:00
> 18/11/2017 00:00
> 18/11/2017 03:00
> 18/11/2017 06:00
> 18/11/2017 09:00
> 18/11/2017 12:00
> 18/11/2017 15:00
> 18/11/2017 18:00
> 18/11/2017 21:00
> 19/11/2017 00:00
> 19/11/2017 03:00
> 19/11/2017 06:00
> 19/11/2017 09:00
> 19/11/2017 12:00
> 19/11/2017 15:00
> 19/11/2017 18:00
> 19/11/2017 21:00
> 20/11/2017 00:00
> 20/11/2017 03:00
> 20/11/2017 06:00
> 20/11/2017 09:00
> 20/11/2017 12:00
> 20/11/2017 15:00
> 20/11/2017 18:00
> 20/11/2017 21:00
> 21/11/2017 00:00
> 21/11/2017 03:00
> 21/11/2017 06:00
> 21/11/2017 09:00
> 21/11/2017 12:00
> 21/11/2017 15:00
> 21/11/2017 18:00
> 22/11/2017 03:00
> 22/11/2017 06:00
> 22/11/2017 09:00
> 22/11/2017 12:00
> 22/11/2017 15:00
> 22/11/2017 18:00
> 22/11/2017 21:00
> 23/11/2017 00:00
> 23/11/2017 03:00
> 23/11/2017 06:00
> 23/11/2017 09:00
> 23/11/2017 12:00
> 23/11/2017 15:00
> 23/11/2017 18:00
> 23/11/2017 21:00
> 24/11/2017 00:00
> 24/11/2017 03:00
> 24/11/2017 06:00
> 24/11/2017 09:00
> 24/11/2017 12:00
> 24/11/2017 15:00
> 24/11/2017 18:00
> 24/11/2017 21:00
> 25/11/2017 00:00
> 25/11/2017 03:00
> 25/11/2017 06:00
> 25/11/2017 09:00
> 25/11/2017 12:00
> 25/11/2017 15:00
> 25/11/2017 18:00
> 25/11/2017 21:00
> 26/11/2017 00:00
> 26/11/2017 03:00
> 26/11/2017 06:00
> 26/11/2017 09:00
> 26/11/2017 12:00
> 26/11/2017 15:00
> 26/11/2017 18:00
> 26/11/2017 21:00
> 27/11/2017 03:00
> 27/11/2017 06:00
> 27/11/2017 09:00
> 27/11/2017 12:00
> 27/11/2017 15:00
> 27/11/2017 18:00
> 27/11/2017 21:00
> 28/11/2017 06:00
> 28/11/2017 09:00
> 28/11/2017 12:00
> 28/11/2017 15:00
> 28/11/2017 18:00
> 28/11/2017 21:00
> 29/11/2017 00:00
> 29/11/2017 03:00
> 29/11/2017 06:00
> 29/11/2017 09:00
> 29/11/2017 12:00
> 29/11/2017 15:00
> 29/11/2017 18:00
> 29/11/2017 21:00
> 30/11/2017 00:00
> 30/11/2017 03:00
> 30/11/2017 06:00
> 30/11/2017 09:00
> 30/11/2017 12:00
> 30/11/2017 15:00
> 30/11/2017 18:00
> 30/11/2017 21:00
> 
> 
> This is the value tha a have using pandas:
> 
> 
> print(data)
> 
> 
> 0     2017-01-11 00:00:00
> 1     2017-01-11 03:00:00
> 2     2017-01-11 06:00:00
> 3     2017-01-11 09:00:00
> 4     2017-01-11 12:00:00
>  ...
> 228   2017-11-30 09:00:00
> 229   2017-11-30 12:00:00
> 230   2017-11-30 15:00:00
> 231   2017-11-30 18:00:00
> 232   2017-11-30 21:00:00
> 
> Please, how can I get four arrays for day, month, year and hour this
> column of my excel.

df["year"] = df["timestamp"].apply(lambda ts: ts.year)

A self-contained demonstration:
$ cat tmp.py
import pandas as pd
import operator

# Create sample data.
df = pd.DataFrame({
    "timestamp": pd.date_range("2020-01-01 01:00", periods=5)
})
print(df)

# Extract "year" etc. attributes from the "timestamp" column
# into the year etc. columns.
for name in "year month day hour".split():
    df[name] = df["timestamp"].apply(operator.attrgetter(name))

# Remove "timestamp" column.
del df["timestamp"]

print(df)
$ python3 tmp.py
            timestamp
0 2020-01-01 01:00:00
1 2020-01-02 01:00:00
2 2020-01-03 01:00:00
3 2020-01-04 01:00:00
4 2020-01-05 01:00:00

[5 rows x 1 columns]
   year  month  day  hour
0  2020      1    1     1
1  2020      1    2     1
2  2020      1    3     1
3  2020      1    4     1
4  2020      1    5     1

[5 rows x 4 columns]
$ 






More information about the Python-list mailing list