简体   繁体   English

从excel中提取日期并使用python将其追加到列表中

[英]Extract Date from excel and append it in a list using python

I have an column in excel which has dates in the format ''17-12-2015 19:35". How can I extract the first 2 digits as integers and append it to a list? In this case I need to extract 17 and append it to a list. Can it be done using pandas also? 我在excel中有一列,其日期格式为“ 17-12-2015 19:35”。如何将前两位数字提取为整数并将其附加到列表中?在这种情况下,我需要提取17和将其添加到列表中,也可以使用熊猫吗?

Code thus far: 到目前为止的代码:

import pandas as pd
Location = r'F:\Analytics Materials\files\paymenttransactions.csv'
df = pd.read_csv(Location)
time = df['Creation Date'].tolist()
print (time)

You could extract the day of each timestamp like 您可以提取每个时间戳记的日期,例如

from datetime import datetime
import pandas as pd

location = r'F:\Analytics Materials\files\paymenttransactions.csv'
df = pd.read_csv(location)

timestamps = df['Creation Date'].tolist()
dates = [datetime.strptime(timestamp, '%d-%m-%Y %H:%M') for timestamp in timestamps]
days = [date.strftime('%d') for date in dates]

print(days)

The '%d-%m-%Y %H:%M' and '%d' bits are format specififers, that describe how your timestamp is formatted. '%d-%m-%Y %H:%M''%d'位是格式说明符,用于描述时间戳的格式。 See eg here for a complete list of directives. 请参阅此处的指令完整列表。

datetime.strptime parses a string into a datetime object using such a specifier. datetime.strptime使用这样的说明符将字符串解析为datetime对象。 dates will thus hold a list of datetime instances instead of strings. dates将因此包含datetime实例列表,而不是字符串。

datetime.strftime does the opposite: It turns a datetime object into string, again using a format specifier. datetime.strftime作用与此相反:再次使用格式说明符将datetime对象转换为字符串。 %d simply instructs strftime to only output the day of a date. %d仅指示strftime仅输出日期中的日期。

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

相关问题 从python中的excel文件名中提取日期 - Extract date from excel file name in python Append数据从列表到excel使用openpyxl - Append data from list to excel using openpyxl 代码从mongodb集合中提取文本字段,并使用pymongo将其附加到python中的列表中 - Code to extract text fields from a mongodb collection and append it to a list in python using pymongo 使用Python从Excel中提取列 - Extract columns from Excel using Python 从其他网址提取数据,然后使用lxml将答案附加到列表中 - extract data from a different url and append the answer to a list using lxml 如何使用 Python 从文件名中提取日期? - How to extract date from the filename using Python? 解析 Excel 单元格中的文本并使用 python 提取日期 - Parse a text in an Excel cell and extract date using python 如何从网页的下拉列表中提取数据并使用python在excel中打印 - How to extract data from dropdown list on a web page and print in excel using python 如何从合并单元格中使用 pandas 将 excel 列数据提取到 python 列表中 - How to extract excel column data into python list using pandas from merged cell 如何从python字典中提取列表(作为dict值)并将其附加到具有字典列表的列表中? - How to extract and append the list(as dict values) from python dictionary to a list having an list of dictionary?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM