Add column to pandas without headers

2024/10/9 12:34:08

How does one append a column of constant values to a pandas dataframe without headers? I want to append the column at the end.

With headers I can do it this way:

df['new'] = pd.Series([0 for x in range(len(df.index))], index=df.index)
Answer

Each not empty DataFrame has columns, index and some values.

You can add default column value and create new column filled by scalar:

df[len(df.columns)] = 0

Sample:

df = pd.DataFrame({0:[1,2,3],1:[4,5,6]})print (df)0  1
0  1  4
1  2  5
2  3  6df[len(df.columns)] = 0
print (df)0  1  2
0  1  4  0
1  2  5  0
2  3  6  0

Also for creating new column with name the simpliest is:

df['new'] = 1
https://en.xdnf.cn/q/70022.html

Related Q&A

Replace NaN values of pandas.DataFrame with values from list

In a python script using the library pandas, I have a dataset of lets say 100 lines with a feature "X", containing 36 NaN values, and a list of size 36.I want to replace all the 36 missing va…

Boring Factorials in python

I am trying to understand and solve the following problem :Sameer and Arpit want to overcome their fear of Maths and so they have been recently practicing Maths problems a lot. Aman, their friendhas be…

The flask host adress in docker run

I want to run a flask application in Docker, with the flask simple http server. (Not gunicorn)I got a host setting problem. In the flask app.py, it should be work as the official tutorial, but it doesn…

Extracting text from pdf using Python and Pypdf2

I want to extract text from pdf file using Python and PYPDF package. This is my pdf fie and this is my code:import PyPDF2 opened_pdf = PyPDF2.PdfFileReader(test.pdf, rb)p=opened_pdf.getPage(0)p_text= p…

Is it possible to change turtles pen stroke?

I need to draw a bar graph using Pythons turtle graphics and I figured it would be easier to simply make the pen a thick square so I could draw the bars like that and not have to worry about making doz…

How to make a local Pypi mirror without internet access and with search available?

Im trying to make a complete local Pypi repository mirror with pip search feature on a server I can only connect an external hard drive to. To be clear, I dont want a simple caching system, the server …

Turn an application or script into a shell command

When I want to run my python applications from commandline (under ubuntu) I have to be in the directory where is the source code app.py and run the application with commandpython app.pyHow can I make i…

pytest - monkeypatch keyword argument default

Id like to test the default behavior of a function. I have the following:# app/foo.py DEFAULT_VALUE = hellodef bar(text=DEFAULT_VALUE):print(text)# test/test_app.py import appdef test_app(monkeypatch):…

How remove a program installed with distutils?

I have installed a python application with this setup.py:#!/usr/bin/env pythonfrom distutils.core import setup from libyouandme import APP_NAME, APP_DESCRIPTION, APP_VERSION, APP_AUTHORS, APP_HOMEPAGE,…

How to check which line of a Python script is being executed?

Ive got a Python script which is running on a Linux server for hours, crunching some numbers for me. Id like to check its progress, so Id like to see what line is being executed right now. If that was …