我有一个csv文件名timestamp.csv,其中第一列是时间.我需要更改每列中时间的格式



csv中的时间格式类似于2022-05-12 10:38:21 594.666,但我需要将其更改为2022-05-112 10:38:21.594666。我需要替换此表达式%Y-%m-%d%H:%m:%S%f的所有值我被困在这里

import pandas as pd
a = pd.read_csv("timestamp.csv")
df=pd.DataFrame(a,columns=['Time'])
df=df.replace([','],'')

此代码不会更改我的格式。

我希望这个10:38:21 594.666格式在所有时间行中更改为10:38:21.594666

一种方法是将其分解为两个易于处理的部分,然后将它们重新组合在一起:

给定:

timestamp
0  2022-05-12 10:38:21 594.666

操作:

# Split into two cols:
df[['timestamp', 'ms']] = df.timestamp.str.split(' (?=S+$)', expand=True)
# Process the timestamp:
df.timestamp = pd.to_datetime(df.timestamp)
# Process the Milliseconds:
df.ms = pd.to_timedelta(df.ms.astype(float), unit='ms')
# Combine them again:
df.timestamp = df['timestamp'] + df['ms']
# Drop our helper column:
df = df.drop('ms', axis=1)
print(df)

输出:

timestamp
0 2022-05-12 10:38:21.594666

由于时间列不是官方的时间格式,我只会将其全部视为字符串,然后使用字符串替换。它不漂亮,但很管用。

import pandas as pd
# input filename
filename = "testdata.txt"
# explicitly force column datatypes to string
col_types = {
"Time": str,
"othercolumn1": str,
"othercolumn2": str,
"etc": str
}
# read csv file
df = pd.read_csv(filename, sep=',', dtype=col_types)
# it ain't pretty but it works
df['Time'] = df['Time'].str.replace('.', '', regex=False)
df['Time'] = df['Time'].str.replace(' ', '.', regex=False) # replace ALL spaces
df['Time'] = df['Time'].str.replace('.', ' ', 1, regex=False) # replace FIRST dot only
# csv write new output
df.to_csv("output.txt", sep=',', header=True, index=False)

最新更新