0

I have 2 data frames. I would like to get below new data frame from 2 data frames.

Desired output 

dataFrameName  no.rows   no.cols
df1Name          100      34
df2Name          212      16

I have tried like below but getting error

def dfFun(*alldfs):
    df = pd.DataFrame(columns=[['dataFrameName ','no.rows','no.cols']], index=[0])
    for i in alldfs:
        df['dataFrameName'] = i
        df['no.rows'] = i.shape[0] # getting error here
        df['no.cols'] = i.shape[1]
        

Function calling

dfFun('df1Name','df2Name')

Error

AttributeError: 'str' object has no attribute 'shape'

I have understood the error but couldn't able to get the desired output.

martineau
  • 119,623
  • 25
  • 170
  • 301

1 Answers1

0

The error you get comes with the way you call your function.

dfFun('df1Name','df2Name')

The use of quotation marks here means instead of inputting two dataframe variables you are instead inputting two strings. Therefore when calling

df['no.rows'] = i.shape[0]

you get the error

AttributeError: 'str' object has no attribute 'shape'

as you're trying to get the shape of a string not of a dataframe.

I understand you also want to store the variable name of the dataframe. To do this you should make the following tweak as seen here by jfs.

However, I believe if you have lots of variables this could possibly add unwanted overhead as you are needing to search through all the variables. Therefore, there may be a nicer way to keep track of the dataframe.

#get variable name
def namestr(obj, namespace): 
    return [name for name in namespace if namespace[name] is obj]

def dfFun(*alldfs):
    df = pd.DataFrame(columns=[['dataFrameName ','no.rows','no.cols']], index=[0])
    for i in alldfs:
        df['dataFrameName'] = namestr(i, globals())[0]
        df['no.rows'] = i.shape[0] # getting error here
        df['no.cols'] = i.shape[1]

Call this function as follows

dfFun(df1, df2)

Where, df1 and df2 are just pandas dataframes.

Dan
  • 103
  • 1
  • 8
  • is there any easy way instead of using other function? – crazycoders Jan 17 '22 at 22:03
  • If you are referring to the `namestr` function then you could replace where the function is called with the list comprehension but I think it is probably easiest just to include what I’ve shown above. I hope that helps - If you’ve found this helpful please accept it as the answer. – Dan Jan 18 '22 at 00:06
  • Dan , can you explain why we using globals() while calling namestr function – crazycoders Jan 25 '22 at 11:08
  • `globals()` returns all information related to the global scope of the program (including variable names). Try running the command by itself to see for yourself. For some more info you could read [this summary](https://www.geeksforgeeks.org/global-local-variables-python/) – Dan Jan 26 '22 at 17:00