How to print dataframe name in title of a plot?
I found nice function here: (Get the name of a pandas DataFrame)
def get_df_name(df):
name =[x for x in globals() if globals()[x] is df][0]
return name
It will help you.
def plot_dist(df,col):
ax=sns.countplot(x=col,data=df)
ax.set_title(get_df_name(df))
How can I get the name of a DataFrame in Python?
Since Python allows you to assign arbitrary attribute names to objects, you can assign an attribute named name
to your dataframe to represent the name of it:
import pandas as pd
df = pd.DataFrame()
df.name = 'My Data Frame'
print(df.name) # My Data Frame
In your case, after you define a name
attribute for dataAllR
:
dataAllR.name = 'dataAllR'
You would use:
exportPath = exportPath + '\\final_py_upload' + data.name + '.csv'
Or, even better:
exportPath = f'{exportPath}\\final_py_upload{data.name}.csv'
Get name of colums where true in pandas dataframe
Try with melt
then groupby
out_d = df.melt('id').query('value').groupby('id')['variable'].agg(list).to_dict()
Out[127]: {1: ['a'], 2: ['a', 'b'], 3: ['c'], 4: ['a', 'c'], 5: ['b', 'c']}
who to get name of column from dataframe automatically
Suppose the following dataframe:
df = pd.DataFrame({'Q1': list('YYYN'), 'Q2': list('NNYY'), 'Q3': list('YNNN')})
print(df)
# Output
Q1 Q2 Q3
0 Y N Y
1 Y N N
2 Y Y N
3 N Y N
Use melt
and pivot_table
to reshape your dataframe:
out = (df.melt(var_name='question', value_name='answer').assign(dummy=1)
.pivot_table('dummy', 'question', 'answer', aggfunc='count')
.rename_axis(columns=None).reset_index())
print(out)
# Output
question N Y
0 Q1 1 3
1 Q2 2 2
2 Q3 3 1
The dummy
variable is set to allow pivot_table
to count value 'Yes' or 'No'.
How to get the original variable name from a list of pandas dataframes
You can actually set a name method for your dataframes.
testFrame1 = pd.DataFrame(columns=["test1"])
testFrame1.name = 'testFrame1'
testFrame2 = pd.DataFrame(columns=["test2"])
testFrame2.name = 'testFrame2'
listOfFrames = [testFrame1,testFrame2]
for frame in listOfFrames:
print(frame.name)
This will print the dataframe names
Related Topics
Why Can a Python Dict Have Multiple Keys with the Same Hash
Which Is More Preferable to Use: Lambda Functions or Nested Functions ('Def')
How to Set Folder Permissions in Windows
Pytz Localize VS Datetime Replace
What's the Best Practice Using a Settings File in Python
Handle Flask Requests Concurrently with Threaded=True
How to Set Window Size in Selenium Chrome Python
How to Count the Nan Values in a Column in Pandas Dataframe
How to Get a Complete List of Object's Methods and Attributes
Nonlocal Keyword in Python 2.X
Python - Windows Shutdown Events
How to Transpose Dataframe in Pandas Without Index
What Are Data Classes and How Are They Different from Common Classes