“Fügen Sie Pandas DataFrame eine neue Spalte hinzu” Code-Antworten

So fügen Sie einer Pandas df eine Spalte hinzu

#using the insert function:
df.insert(location, column_name, list_of_values) 
#example
df.insert(0, 'new_column', ['a','b','c'])
#explanation:
#put "new_column" as first column of the dataframe
#and puts 'a','b' and 'c' as values

#using array-like access:
df['new_column_name'] = value

#df stands for dataframe
Annoyed Antelope

Python So fügen Sie Spalten zu einem Pandas -Datenframe hinzu

# Basic syntax:
pandas_dataframe['new_column_name'] = ['list', 'of', 'column', 'values']

# Note, the list of column values must have length equal to the number
# 	of rows in the pandas dataframe you are adding it to.

# Add column in which all rows will be value:
pandas_dataframe['new_column_name'] = value
# Where value can be a string, an int, a float, and etc 
Charles-Alexandre Roy

Fügen Sie die Datendrame -Spalte hinzu, um festgelegt zu werden


In [79]:

df
Out[79]:
         Date, Open, High,  Low,  Close
0  01-01-2015,  565,  600,  400,    450
In [80]:

df['Name'] = 'abc'
df
Out[80]:
         Date, Open, High,  Low,  Close Name
0  01-01-2015,  565,  600,  400,    450  abc

Sleepy Swan

Fügen Sie Pandas DataFrame eine neue Spalte hinzu

# Import pandas package
import pandas as pd
 
# Define a dictionary containing Students data
data = {'Name': ['Jai', 'Princi', 'Gaurav', 'Anuj'],
        'Height': [5.1, 6.2, 5.1, 5.2],
        'Qualification': ['Msc', 'MA', 'Msc', 'Msc']}
 
# Convert the dictionary into DataFrame
df = pd.DataFrame(data)
 
# Declare a list that is to be converted into a column
address = ['Delhi', 'Bangalore', 'Chennai', 'Patna']
 
# Using 'Address' as the column name
# and equating it to the list
df['Address'] = address
 
# Observe the result
df
Sibahle Molphy

Ähnliche Antworten wie “Fügen Sie Pandas DataFrame eine neue Spalte hinzu”

Fragen ähnlich wie “Fügen Sie Pandas DataFrame eine neue Spalte hinzu”

Weitere verwandte Antworten zu “Fügen Sie Pandas DataFrame eine neue Spalte hinzu” auf Python

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen