Home > front end >  Create an unnamed column pandas
Create an unnamed column pandas

Time:10-26

How to append to an existing dataframe an unnamed column with random data?

df.insert requires a name of column
df['column_name'] = ... obviously require column name too

CodePudding user response:

As per panda's official documentation,

columns: Index or array-like

Column labels to use for resulting frame when data does not have them, defaulting to RangeIndex(0, 1, 2, …, n). If data contains column labels, will perform column selection instead.

If a column does not have a column label, it would be assigned one. So, even if you try something like this, creating a series with no name and concatenating it to a df:

df=pd.DataFrame({"value":[1,2,3]})
column_to_add = pd.Series([4,5,6])
pd.concat([df,column_to_add],axis=1)

The resulting column would still be automatically assigned a column label of 0.

  • Related