Python Pandas 单元问题更新时间
Python Pandas Update Time in Cell Issues
疯狂尝试更新数据框中的一列时间条目。我正在打开一个 csv 文件,其中有一列 UTC 时间条目。我可以把这些时间转换成阿拉斯加标准时间,然后打印出新的时间就好了。但是,当我尝试将时间放回数据帧时,虽然我没有收到任何错误,但我也没有在数据帧中获得新时间。保留旧的 UTC 时间。代码在下面,我很好奇我错过了什么。时间有什么特别之处吗?
import glob
import os
import pandas as pd
from datetime import datetime
from statistics import mean
def main():
AKST = 9
allDirectories = os.listdir('c:\MyDir\')
for directory in allDirectories:
curDirectory = directory.capitalize()
print('Gathering data from: ' + curDirectory)
dirPath = 'c:\MyDir\' + directory + '\*.csv'
# Files are named by date, so sorting by name gives us a proper date order
files = sorted(glob.glob(dirPath))
df = pd.DataFrame()
for i in range(0,len(files)):
data = pd.read_csv(files[i], usecols=['UTCDateTime', 'current_humidity', 'pm2_5_cf_1', 'pm2_5_cf_1_b'])
dfTemp = pd.DataFrame(data) # Temporary dataframe to hold our new info
df = pd.concat([df, dfTemp], axis=0) # Add new info to end of dataFrame
print("Converting UTC to AKST, this may take a moment.")
for index, row in df.iterrows():
convertedDT = datetime.strptime(row['UTCDateTime'], '%Y/%m/%dT%H:%M:%Sz') - pd.DateOffset(hours=AKST)
print("UTC: " + row['UTCDateTime'])
df.at[index,'UTCDateTime'] = convertedDT
print("AKST: " + str(convertedDT))
print("row['UTCDateTime] = " + row['UTCDateTime'] + '\n') # Should be updated with AKST, but is not!
编辑 - 或者:有没有办法将首次读入数据框的日期转换为日期?看起来这比有两个 for 循环要快。
从您的代码来看,数据似乎已在数据框中正确更新,但您正在打印未更新的行,因为它是从数据框中获取的在更新之前!
#You are updating df
df.at[index,'UTCDateTime'] = convertedDT #You are updating df
# below you are printing row
print("row['UTCDateTime] = " + row['UTCDateTime']
请参阅下面的示例代码及其输出以获取解释。
data=pd.DataFrame({'Year': [1982,1983], 'Statut':['Yes', 'No']})
for index, row in data.iterrows():
data.at[index, 'Year'] = '5000' + str(index)
print('Printing row which is unchanged : ', row['Year'])
print('Updated Dataframe\n',data)
输出
Printing row which is unchanged : 1982
Printing row which is unchanged : 1983
Updated Dataframe
Year Statut
0 50000 Yes
1 50001 No
疯狂尝试更新数据框中的一列时间条目。我正在打开一个 csv 文件,其中有一列 UTC 时间条目。我可以把这些时间转换成阿拉斯加标准时间,然后打印出新的时间就好了。但是,当我尝试将时间放回数据帧时,虽然我没有收到任何错误,但我也没有在数据帧中获得新时间。保留旧的 UTC 时间。代码在下面,我很好奇我错过了什么。时间有什么特别之处吗?
import glob
import os
import pandas as pd
from datetime import datetime
from statistics import mean
def main():
AKST = 9
allDirectories = os.listdir('c:\MyDir\')
for directory in allDirectories:
curDirectory = directory.capitalize()
print('Gathering data from: ' + curDirectory)
dirPath = 'c:\MyDir\' + directory + '\*.csv'
# Files are named by date, so sorting by name gives us a proper date order
files = sorted(glob.glob(dirPath))
df = pd.DataFrame()
for i in range(0,len(files)):
data = pd.read_csv(files[i], usecols=['UTCDateTime', 'current_humidity', 'pm2_5_cf_1', 'pm2_5_cf_1_b'])
dfTemp = pd.DataFrame(data) # Temporary dataframe to hold our new info
df = pd.concat([df, dfTemp], axis=0) # Add new info to end of dataFrame
print("Converting UTC to AKST, this may take a moment.")
for index, row in df.iterrows():
convertedDT = datetime.strptime(row['UTCDateTime'], '%Y/%m/%dT%H:%M:%Sz') - pd.DateOffset(hours=AKST)
print("UTC: " + row['UTCDateTime'])
df.at[index,'UTCDateTime'] = convertedDT
print("AKST: " + str(convertedDT))
print("row['UTCDateTime] = " + row['UTCDateTime'] + '\n') # Should be updated with AKST, but is not!
编辑 - 或者:有没有办法将首次读入数据框的日期转换为日期?看起来这比有两个 for 循环要快。
从您的代码来看,数据似乎已在数据框中正确更新,但您正在打印未更新的行,因为它是从数据框中获取的在更新之前!
#You are updating df
df.at[index,'UTCDateTime'] = convertedDT #You are updating df
# below you are printing row
print("row['UTCDateTime] = " + row['UTCDateTime']
请参阅下面的示例代码及其输出以获取解释。
data=pd.DataFrame({'Year': [1982,1983], 'Statut':['Yes', 'No']})
for index, row in data.iterrows():
data.at[index, 'Year'] = '5000' + str(index)
print('Printing row which is unchanged : ', row['Year'])
print('Updated Dataframe\n',data)
输出
Printing row which is unchanged : 1982
Printing row which is unchanged : 1983
Updated Dataframe
Year Statut
0 50000 Yes
1 50001 No