Anonymous Anonymous - 1 month ago
138 0

Pandas problem now solved

Python

Pandas Solved

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.set_index(["Dato"],inplace=True)
    #print(content)
    #content=content.sort_values(by=['Dato'],ascending=[1])

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

    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=indeks["Dato"], columns=[periods]) # create new,empty dataframe with columns = each invoiceing period

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

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

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

        # -Start work on filling the new dataframe
        to_input=content[content["Periode"]== periods[i]]
        to_input.set_index("Dato",inplace=True)
        #print(to_input.index)
        #print(df.index)

        
        df.loc[df.index.isin(to_input.index),periods[i]]= to_input["Restbeløp"]
        
    print(df)

if __name__ == "__main__":
       create_graph()