How to (re)name an empty column header in a pandas dataframe without exporting to csv

2024/10/5 17:20:56

I have a pandas dataframe df1 with an index column and an unnamed series of values. I want to assign a name to the unnamed series.

The only way to do this that I know so far is to export to df1.csv using:

df1.to_csv("df1.csv", header = ["Signal"])

and then re-import using:

pd.read_csv("df1.csv", sep=",")

However, this costs time and storage space. How to do this in-memory?

When I do df2 = df1.rename(columns = {"" : "Signal"}, inplace = True)

I yield:

AttributeError: "Series" object has no attribute "Signal".

Answer

I think inplace=True has to be removed, because it return None:

df2 = df1.rename(columns = {"" : "Signal"})

df1.rename(columns = {"" : "Signal"}, inplace = True)

Another solution is asign new name by position:

df.columns.values[0] = 'Signal'

Sample:

df1 = pd.DataFrame({'':[1,2,3],'B':[4,5,6],'C':[7,8,9]})print (df1)B  C
0  1  4  7
1  2  5  8
2  3  6  9df2 = df1.rename(columns = {"" : "Signal"})
print (df2)Signal  B  C
0       1  4  7
1       2  5  8
2       3  6  9
https://en.xdnf.cn/q/70456.html

Related Q&A

Capturing the video stream from a website into a file

For my image classification project I need to collect classified images, and for me a good source would be different webcams around the world streaming video in the internet. Like this one:https://www.…

Recreating time series data using FFT results without using ifft

I analyzed the sunspots.dat data (below) using fft which is a classic example in this area. I obtained results from fft in real and imaginery parts. Then I tried to use these coefficients (first 20) to…

python BeautifulSoup get all href in Children of div

I am new to python and Ive been trying to get links and inner text from this html code : <div class="someclass"><ul class="listing"><li><a href="http://lin…

Python TypeError: sort() takes no positional arguments

I try to write a small class and want to sort the items based on the weight. The code is provided, class Bird:def __init__(self, weight):# __weight for the private variableself.__weight = weightdef wei…

Trouble with basemap subplots

I need to make a plot with n number of basemap subplots. But when I am doing this the all the values are plotted on the first subplot.My data is a set of n matrixes, stored in data_all.f, map = plt.sub…

Remove circular references in dicts, lists, tuples

I have this following really hack code which removes circular references from any kind of data structure built out of dict, tuple and list objects.import astdef remove_circular_refs(o):return ast.liter…

how to change image format when uploading image in django?

When a user uploads an image from the Django admin panel, I want to change the image format to .webp. I have overridden the save method of the model. Webp file is generated in the media/banner folder b…

Write info about nodes to a CSV file on the controller (the local)

I have written an Ansible playbook that returns some information from various sources. One of the variables I am saving during a task is the number of records in a certain MySQL database table. I can p…

Python minimize function: passing additional arguments to constraint dictionary

I dont know how to pass additional arguments through the minimize function to the constraint dictionary. I can successfully pass additional arguments to the objective function.Documentation on minimiz…

PyQt5 triggering a paintEvent() with keyPressEvent()

I am trying to learn PyQt vector painting. Currently I am stuck in trying to pass information to paintEvent() method which I guess, should call other methods:I am trying to paint different numbers to a…