使用globals()
并搜索与局部变量匹配的对象data
#### Test Code #### # Import pandas module import pandas as pd # Create DataFrame df = pd.DataFrame({"A":[1,2,3], "B":[10,20,30]}) # Define Function def test(data): name =[x for x in globals() if globals()[x] is data][0] print("Dataframe Name is: %s" % name) print(data.describe()) # Run Function test(df)
结果将是:
Dataframe Name is: df A B count 3.0 3.0 mean 2.0 20.0 std 1.0 10.0 min 1.0 10.0 25% 1.5 15.0 50% 2.0 20.0 75% 2.5 25.0 max 3.0 30.0