Python Forum
Need help formatting dataframe data before saving to CSV
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help formatting dataframe data before saving to CSV
#1
I am trying to parse and save my IG messages for stat purposes.

I have the following code that works and saves to a csv file, but when i open the file each line is wrapped in " " like so ("2022-06-29,08:50 AM,Joe,Liked a message"), so when i try to import the data into excel, its not split out correctly, thats issue #1


Issue #2 is how the date is formatted, either im not choosing the correct format below or just missing something, but the date that is placed in the file is formatter as the below code shows yyyy-mm-dd i need to have it formatted in mm/dd/yyyy

Issue #3 same thing with the time, in this case i need to include the seconds in the format, currently the below returns 07:05 PM, but need it to return 07:05:00 PM

Can someone suggest what im doing wrong or offer some samples that i can change or implement in my code to clean up the results some more.
import json
from datetime import datetime
import pandas as pd
import os

f = open('message_1.json')

data = json.load(f)

lv = []

for message in data["messages"]:
    timestamp = datetime.fromtimestamp(message["timestamp_ms"] / 1000)
    
    date_val = timestamp.strftime('%Y-%m-%d')
    time_val = timestamp.strftime("%I:%M %p")
       
    if 'content' not in message:
        st = date_val + "," + time_val + "," + message["sender_name"] + "," + ""
        lv.append(st)

    else:
        st = date_val +"," + time_val + "," + message["sender_name"] + "," + message["content"]
        lv.append(st)

df = pd.DataFrame(lv)
Reply


Messages In This Thread
Need help formatting dataframe data before saving to CSV - by cubangt - Jun-29-2022, 08:32 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Filter data into new dataframe as main dataframe is being populated cubangt 8 1,041 Oct-23-2023, 12:43 AM
Last Post: cubangt
  Seeing al the data in a dataframe or numpy.array Led_Zeppelin 1 1,175 Jul-11-2022, 08:54 PM
Last Post: Larz60+
  Problem in saving .xlsm (excel) file using pandas dataframe in python shantanu97 2 4,376 Aug-29-2021, 12:39 PM
Last Post: snippsat
  Reading data to python: turn into list or dataframe hhchenfx 2 5,423 Jun-01-2021, 10:28 AM
Last Post: Larz60+
  How to save json data in a dataframe shantanu97 1 2,179 Apr-15-2021, 02:44 PM
Last Post: klllmmm
  Formatting date in a dataframe WiPi 1 1,783 Jan-06-2021, 11:26 AM
Last Post: WiPi
  Pandas Extract data from two dataframe nio74maz 1 2,206 Dec-26-2020, 09:52 PM
Last Post: nio74maz
  Error when Excelwriter saving a dataframe with datetime datatype with timezone klllmmm 3 13,549 Dec-08-2020, 11:37 AM
Last Post: Larz60+
  saving data from text file to CSV file in python having delimiter as space K11 1 2,430 Sep-11-2020, 06:28 AM
Last Post: bowlofred
  Formatting Data/Time with Pyodbc and openpyxl bearcats6001 0 2,308 Aug-17-2020, 03:44 PM
Last Post: bearcats6001

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020