Convert dtype object to numeric (pandas)

xo1b Source

I'm quite new to programming in general and as I'm teacher I'm trying to figuring out which subjects could be teaches using python. So that's what I wanted: a pandas dataframe where each new row (object) is generated by user input. for a start each new row has 6 values, 4 of them are user input and two are calculations. So I came up with this:

import numpy as np
import pandas as pd

av_data_test=pd.DataFrame(columns=['Nr', 'Beleg', 'Nutzungsdauer', 'Anschaffungswert', 'Abschreibung', 'Buchwert'])
av_data_test['Nr']=av_data_test.index
av_data_test['Anschaffungswert'].apply(pd.to_numeric)
av_data_test['Nutzungsdauer'].apply(pd.to_numeric)
av_data_test['Abschreibung'].apply(pd.to_numeric)
av_data_test['Buchwert'].apply(pd.to_numeric)

class Anlage():
    def __init__(self, new_asset, new_line):
        self.name=name
        self.new_asset=new_asset
        self.new_line=new_line

    def new_asset():
        anlegen=str(input('Wollen Sie eine neue Anlage anlegen (=j) oder das Anlagenerzeichnis sehen(=n)?'))
        if anlegen == 'j': 
            new=Anlage.new_line()
        else: 
            return av_data_test

    def new_line():
        wert=av_data_test.iloc[:, av_data_test.columns.get_loc('Anschaffungswert')]
        nd=av_data_test.iloc[:, av_data_test.columns.get_loc('Nutzungsdauer')]     
        av_data_test['Abschreibung']=
             av_data_test['Abschreibung'].astype(float)
        av_data_test['Buchwert']=
             av_data_test['Buchwert'].astype(float)

        def afa(wert, nd): 
            global abschr
            abschr=wert/nd
            return abschr

        def bw(wert, abschr):
            buchw=wert-abschr


        for x in np.arange(0, 1, dtype=None):
            av_data_test.loc[x] = [int(input('nr')), str(input('beleg')), float(input('nd')), float(input('wert')),
                                     afa(wert, nd), bw(wert, abschr)]
            return av_data_test.loc[x]

Anlage.new_asset()
av_data_test.dtypes

if I run it the columns where I wanted to do the calculations are still dtype=object. I don't manage to convert them to int or float (tried pd.to_numeric, df.apply(pd.to_numeric) and df.astype(float). thanks

solved it: the values in the user input lines to 0 and replaced them later with the values of the function

pythonpandasapply

Answers

comments powered by Disqus