How to access data imported using pandas?

I use pandas to import some .dta file and numpy / sklearn to make some statistics on the set. I call the data sampleI do the following:

#   import neccessary packages
import pandas as pd
import numpy as np
import sklearn as skl

#   import data and give a little overview (col = var1-var5, 20 rows)
sample = pd.read_stata('sample_data.dta')
print('variables in dataset')
print(sample.dtypes)
print('first 5 rows and all cols')
print(sample[0:5])

# generate a new var
var6 = sample.var1/sample.var3

I get an error if I directly address a variable by its name ( var1vs. sample.var1). I find it a little tedious to always include sample.. Is there a good way to call variables directly by their name?

+4
source share
1 answer

See this contrived example. I usually don't like messing with locals()and globals(), but I don't see a cleaner way:

class A:
    def __init__(self):
        self.var1 = 1
        self.var2 = 2

obj = A()

locals().update(obj.__dict__)

print(var1)
print(var2)
>> 1
   2

, df.columns __dict__. :

import pandas as pd

df = pd.DataFrame({'a':[1]})

for col in df.columns:
     locals().update({col: df[col]})

print(a)
>> 0    1
   Name: a, dtype: int64

, , , :

import pandas as pd

a = 7

print(a)
>> 7

df = pd.DataFrame({'a':[1]})

for col in df.columns:
     locals().update({col: df[col]})

print(a)
>> 0    1
   Name: a, dtype: int64
+4

Source: https://habr.com/ru/post/1652006/


All Articles