Home > Mobile >  Saving dataframe with separated column in R
Saving dataframe with separated column in R

Time:09-25

I'm trying to separate a column that holds a timestamp of the date and time into 2 columns. However, I have problems with saving it. In this case, the dataframe "four_rows" is being printed in the console, but not being saved as such. I'm struggling to understand how to save it as a dataframe.

mydata <-read.csv("/Users/me/Some_Folder/big_csv_file.csv")
four_rows <- mydata[1:4,]

four_rows %>% separate(Datetime, c('Date', 'Time'), sep=" ")

CodePudding user response:

If we need to automatically update the original object use the magrittr compound operator (%<>%)

library(magrittr)
four_rows %<>%
    separate(Datetime, c('Date', 'Time'), sep=" ")

Now, we check for

four_rows

CodePudding user response:

Go to upload 2 csv files from 2 different url's to a dynamodb table. I am using pandas to get the desired data from the url's and merge the 2 dataframes into a df3. I'm running into an issue when I use put_item to update the database. I have tried converting the pandas series into strings but that doesn't seem to work either. Here is the lambda function:

import csv
import pandas as pd
import io
import requests
import numpy as np
import boto3
from datetime import datetime
import json
from decimal import Decimal

def lambda_handler(event, context): 
    
    url1 = "https://raw.githubusercontent.com/nytimes/covid-19-data/master/us.csv"
    url2 = "https://raw.githubusercontent.com/datasets/covid-19/master/data/time-series-19-covid-combined.csv"
    
    df1 = pd.read_csv(url1)
    df1 = pd.DataFrame(df1)
    df1 = df1.drop(0)

    df2 = pd.read_csv(url2, delimiter=',')
    df2 = pd.DataFrame(df2)
    df2['Recovered'] = df2['Recovered'].fillna(0).astype(np.int64)
    df2 = df2.loc[df2['Country/Region'] == 'US', 'Recovered']
    df2 = df2.reset_index(drop=True)
    df2.index = np.arange(1, len(df2)   1)
    
    df3 = df1.join(df2)
    region = 'eu-west-2'
    
    try:
        dyndb = boto3.client('dynamodb', region_name=region)
        firstrecord = True
        for row in df1:
            if firstrecord:
                firstrecord = False
                continue
            cases = df3['cases']
            date = df3['date']
            deaths = df3['deaths']
            Recovered = df3['Recovered']
        response = dyndb.put_item(TableName='covidstatstable',
                Item={
                'cases': {'N': cases},
                'date': {'S': date},
                'deaths': {'N': deaths},
                'Recovered': {'N': Recovered},
                })

        print('Put succeeded:')
    except Exception as e:
        print(str(e))

and here is the function logs:

  •  Tags:  
  • r csv
  • Related