1

I want to display just the column's header name. I tried using .columns, but that list all the data frame's column headers and I just want to only print the header of the two columns I'm passing.

def test(x, y): # x and y are specific columns within a data frame that is being passed.

   print(x.columns, y.columns) # Runs an error of course, but I want to know how I would display just display their header names.             

So I used panda to create a data frame of a .csv, which could be this:

    num1   num2   num3
0    1      2      3
1    4      5      6
2    7      8      9

So lets say I passed the columns 'num1' and 'num3' as parameters to the function test as x and y. I want to print the names num1 and num3 since those are the header names. How would I go about that?

UPDATE: Okay so, I got the names to display using print(). However, I guess the actual error is when I do plt.xlabel(x.name) & plt.ylabel(y.name). It gives the error: 'str' object has no attribute 'pop'. I thought print() and plt.xlabel() or plt.ylabel() would be treated the same, my apologies.

UPDATE 2: Solved! Thanks guys!

5
  • an example would help people to understand what you want to achieve. Commented Oct 5, 2019 at 21:07
  • 1
    Hello! updated to add an example. Commented Oct 5, 2019 at 21:33
  • If you already know the names, why don't just print them? In your example you say you input 'num1' and 'num3' to print 'num1' and 'num3'. There is no look-up required you already have what you need. Commented Oct 5, 2019 at 21:37
  • Ok, now your question is clearer. Commented Oct 5, 2019 at 21:42
  • @JoeErnst because I have a large list of columns/rows and I want to print multiple graphs. I would like to automate the labels for my graph without having to constantly change the names and just have it place it for me. Commented Oct 5, 2019 at 21:46

2 Answers 2

1
d = {'id': ['x1', 'x2'],'t1': [3,11]}
df = pd.DataFrame(data=d)


def test(x, y): # x and y are specific columns within a data frame that is being passed.
   print(x.name, y.name)
n1 = df['id']
n2 = df['t1']

test(n1,n2)

#output
id t1
Sign up to request clarification or add additional context in comments.

5 Comments

Getting the error: 'str' object has no attribute 'pop'
@researchnewbie how you are calling the function?
So what I did was I pretty much said n1 = df['num1'] and n2 = df['num2'], and then I called the function by doing test(n1, n2). My graph correctly displays the data, just not the names which I want to use as labels for plots.
@researchnewbie I have edited my answer. you can show more code.
Okay, so how would I go about this? I don't really know what to modify from mines to get to this since I can't really recreate the data=d portion. Its over 100k rows and 75+ columns. The thing is with my data, I just entered the .csv and used read_csv, where it organized the data for me. It prints the graph correctly and identifies each header correctly as well, just won't do .name as it works in your code.
1

Given the following dataframe:

df = pd.DataFrame([[1,2,3], [4,5,6], [7,8,9]], columns=['num1', 'num2', 'num3'])

Try this code:

def test(x, y):
    print(x.name, y.name)

test(x=df['num1'], y=df['num3'])

gives:

# output: num1 num3

Note: x and y are pandas series, and to get the name of the series (which corrsipsonde the name of the df column) you must use the attribute .name

If I understand your second question correctly, this code is for you:

cols = df.columns

for x, y in zip(cols, cols[1:]):
    test(df[x],df[y])

and gives:

num1 num2
num2 num3

3 Comments

Do I have to define it like that when making it the arguments? I did it prior by doing this: x = df['num1] , y = df['num2'] and then test(x, y) yet it still doesn't work.
@researchnewbie I updated the answer, let me know if that's what you mean
I updated my answer to display more about whats going on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.