[PYTHON] How to change multiple columns of csv in Pandas (Unixtime-> Japan Time)

import glob
import os
import pandas as pd
import pandas as pd
from datetime import datetime
from pytz import timezone


#Script to change CSV data(column'starttime', 'endtime'Changed from Unix time to Japan time)

#There is a target csv file directly under the program execution location
csv_list = os.listdir()

# !!Timely.remove()Please pull out the non-csv file with.
# !!I don't have time right now, but anyone who can fix it is welcome!
# (↑ Delete from list or not put in list except csv)



tz = timezone('Asia/Tokyo')


for csv_file in csv_list:
    
    
    
    japan_starttime_list = []
    japan_endtime_list = []
    
    
    print(csv_file, "Read...")
    
    df = pd.read_csv(csv_file)
    
    starttime_series = df.starttime
    endtime_series = df.endtime
    
    
    for starttime in starttime_series:
        starttime_str = str(starttime)[:10]
        starttime_int = int(starttime_str)
        
        # Unixtime -> Japan time(str) ->Store in list
        utc_time = datetime.fromtimestamp(starttime_int)
        japan_time = utc_time.astimezone(tz)
        japan_time_str = japan_time.strftime('%H:%M:%S')
        japan_starttime_list.append(japan_time_str)
        
        
    #Do the same with endtime
    for endtime in endtime_series:
        endtime_str = str(endtime)[:10]
        endtime_int = int(endtime_str)
        
        # Unixtime -> Japan time(str) ->Store in list
        utc_time = datetime.fromtimestamp(endtime_int)
        japan_time = utc_time.astimezone(tz)
        japan_time_str = japan_time.strftime('%H:%M:%S')
        japan_endtime_list.append(japan_time_str)
        
    
    #Data change in two columns of dataframe
    df.starttime = japan_starttime_list
    df.endtime = japan_endtime_list
    
    
    #Create a new csv
    df.to_csv('rev_' + csv_file)

Recommended Posts

How to change multiple columns of csv in Pandas (Unixtime-> Japan Time)
How to read CSV files in Pandas
How to display multiple images of galaxies in tiles
How to output CSV of multi-line header with pandas
How to get an overview of your data in Pandas
How to write soberly in pandas
How to calculate the sum or average of time series csv data in an instant
How to handle multiple versions of CUDA in the same environment
How to change the color of just the button pressed in Tkinter
<Pandas> How to handle time series data in a pivot table
[Python] Summary of how to use pandas
How to convert csv to tsv in CLI
How to reassign index in pandas dataframe
How to change editor color in PyCharm
Adding Series to columns in python pandas
[Python] How to read a csv file (read_csv method of pandas module)
Plot CSV of time series data with unixtime value in Python (matplotlib)
How to keep track of work in Powershell
Summary of how to import files in Python 3
Summary of how to use MNIST in Python
Type conversion of multiple columns of pandas DataFrame with astype at the same time
How to read time series data in PyTorch
How to change the appearance of unselected Foreign Key fields in Django's ModelForm
Add totals to rows and columns in pandas
[Java] How to switch between multiple versions of Java
Convert timezoned date and time to Unixtime in Python2.7
How to get multiple model objects randomly in Django
How to get the number of digits in Python
How to write string concatenation in multiple lines in Python
How to measure processing time in Python or Java
How to access with cache when reading_json in pandas
How to convert JSON file to CSV file with Python Pandas
How to change static directory from default in Flask
I tried to summarize how to use pandas in python
[Work efficiency] How to change file names in Python
How to retrieve multiple arrays using slice in python.
How to read csv containing only integers in Python
Summary of how to share state with multiple functions
How to use Pandas 2
How to change python version of Notebook in Watson Studio (or Cloud Pak for Data)
How to find out if there is an arbitrary value in "somewhere" of pandas DataFrame
[Python] How to delete rows and columns in a table (list of drop method options)
[Question] How to get data of textarea data in real time using Python web framework bottle
How to embed multiple embeds in one message with Discord.py
How to find the optimal number of clusters in k-means
How to adapt multiple machine learning libraries in one shot
How to get rid of server custom emoji in message.content
[Go language] How to get terminal input in real time
How to slice a block multiple array from a multiple array in Python
[Updated from time to time] Summary of design patterns in Java
[R] [Python] Memo to read multiple csv files in multiple zip files
How to generate exponential pulse time series data in python
How to define multiple variables in a python for statement
How to develop in a virtual environment of Python [Memo]
Comparison of how to use higher-order functions in Python 2 and 3
How to get a list of built-in exceptions in python
How to change Apache's Document Root in Elastic Beanstalk's .ebextensions
Summary of how to write .proto files used in gRPC
[Python] How to change EXCEL file saved in xlsb to xlsx
Precautions when changing unix time to datetime type in pandas
How to create dataframes and mess with elements in pandas