Anonymous Anonymous - 1 month ago 144 0

Having trouble assigning values to a new dataframe from the old.

Python

Pandas

import os, glob, time
import matplotlib.pyplot as plt
import datetime
import pandas as pd
import process_erate as pe
import numpy as np

current_path = os.path.realpath(__file__).split(__file__)[0]
current_path=current_path.split("\\")
current_path="\\".join(current_path[:-2])+ "\\Fra Lindorff"
inn_path = current_path + "\\INN\\"
backup_path = current_path + "\\processed\\"
final_file=current_path + "\\Lindorff_data.csv"


def create_graph():
    content = pd.read_csv(final_file, engine='python', delimiter=',', usecols=["Dato","Periode","Restbeløp","Løsningsgrad"], header=0, parse_dates=[0], index_col=False, dayfirst=True, infer_datetime_format=True, )

    # -getting data ready
    content = content[pd.notnull(content["Restbeløp"])] # fjerner null verdier
    content.sort_values(by="Dato", inplace=True)
    content.reset_index(inplace=True)
    #print(content)
    #content=content.sort_values(by=['Dato'],ascending=[1])

    # -Create new dataframe with dates as index and columns = invoicing periods
    index=pd.DataFrame(content["Dato"])
    index.drop_duplicates(keep='first', inplace=True)
    index=index["Dato"].tolist()
    #print(index)

    periods=pd.DataFrame(content["Periode"])
    periods=periods.drop_duplicates(keep='first')
    periods.sort_values(by="Periode",inplace=True)
    periods = periods["Periode"].tolist()
    df=pd.DataFrame(index=index, columns=periods) # create new,empty dataframe with columns = each invoiceing period

    # -chekcing
    #print(str(df.columns.values.tolist()))
    #print(df.shape)

    # -processing each column (invoicing period) Column[i]

    for i in range(0,2):  #replace "2" with len(periods)
        #print(i)

        # -Start work on filling the new dataframe
        c_index=content[content["Periode"]== periods[i]].index.tolist()
        to_input=pd.DataFrame(content.loc[c_index], columns=["Dato", "Restbeløp"],copy=True)
        #print(to_input.shape)

        # -try to assign values from each columns to the DF, works with scalar, but how to do this with inputs.
        df.loc[df.index.isin(to_input["Dato"]),periods[i]]=to_input.loc[to_input.index,"Restbeløp"]

    print(df)

if __name__ == "__main__":
    create_graph()
Comments