Setting column names in a pandas dataframe (Python)

2024/10/9 17:24:13

When setting a column name for a pandas dataframe, why does the following work:

df_coeff = pd.DataFrame(data = lm.coef_, index = X.columns, columns = ['Coefficient'])

While this does not work

df_coeff = pd.DataFrame(data = lm.coef_, index = X.columns, columns = 'Coefficient')

Why are brackets required around the column name? What is Python doing here?

Thanks

Answer

The DataFrame constructor expects an "Index or array-like" as the column argument.

  • ['Coefficient'] is a Python list with a single entry, the string 'Coefficient'.
  • 'Coefficient' is not a list, but just a string.

DataFrame accepts a Python list, but not a string, as an array-like.

https://en.xdnf.cn/q/118558.html

Related Q&A

Check that Python function does not modify argument?

You know how in Python, if v is a list or a dictionary, its quite common to write functions that modify v in place (instead of just returning the new value). Im wondering if it is possible to write a c…

What Python 3 version for my Django project

I will try to port my Python 2.7 with Django to Python 3. But now my question is what version is the most stable one today? Ive heard people use 3.2 and 3.4 and recommend it. But now Im asking you guy…

Error during runfile in Eclipse with PyDev/ error initializing console

Using a PyDev console in Eclipse, which initially worked fine. Python code would work inside the console. When I started writing a file within a PyDev module, I tried executing runfile() but the consol…

Reversibly encode two large integers of different bit lengths into one integer

I want to encode two large integers of possibly different maximum bit lengths into a single integer. The first integer is signed (can be negative) whereas the second is unsigned (always non-negative). …

Pytube AttributeError: NoneType object has no attribute span

Hi I have a problem with AttributeError: NoneType object has no attribute span I read on the StackOverflow a channel with this problem on this I found the potential solution but it still not working he…

GDB: ModuleNotFoundError: No module named _tkinter [duplicate]

This question already has answers here:Why does tkinter (or turtle) seem to be missing or broken? Shouldnt it be part of the standard library?(4 answers)Closed last year.So Im trying to debug my C co…

Why can the difference of different floating numbers be 0 in python? [duplicate]

This question already has answers here:Float to Int type conversion in Python for large integers/numbers(2 answers)Closed last year.Why is the result of below code 0 in python3? a = "4.1512940685…

twinx and sns.barplot seaborn are overlapping bars

I would like to use sns.seaborn to display the np.sum and the np.mean on 2 different axes (with ax2 = ax1.twinx() I assume). The probem I have is that the graphs are overlapped and not readable.Am I ap…

Cant get tensorflow on Anaconda [duplicate]

This question already has an answer here:TensorFlow Importing error ( Using Anaconda)(1 answer)Closed 2 years ago.I need tensorflow to run a guided project I did on Coursera, but I am not able to insta…

Extract number between text and | with RegEx Python

I want to extract the information between CVE and |, but only the first time that CVE appear in the txt. I have now the follow code:import re f = open (/Users/anna/PycharmProjects/extractData/DiarioOfi…