current position:Home>Pandas: how to create an empty data frame with column names
Pandas: how to create an empty data frame with column names
2022-01-30 11:56:32 【Stone sword】
You can use the following basic syntax to create an empty column with a specific column name pandas DataFrame.
df = pd.DataFrame(columns=['Col1', 'Col2', 'Col3'])
Copy code
The following example shows how to use this syntax in practice .
example 1: Create a with column names and no rows DataFrame
The following code shows how to create a with a specific column name and no rows pandas DataFrame.
import pandas as pd
#create DataFrame
df = pd.DataFrame(columns=['A', 'B', 'C', 'D', 'E'])
#view DataFrame
df
A B C D E
Copy code
We can use shape To get DataFrame Size .
#display shape of DataFrame
df.shape
(0, 5)
Copy code
This tells us DataFrame Yes 0 Row sum 5 Column .
We can also use **list()** To get a list of column names .
#display list of column names
list(df)
['A', 'B', 'C', 'D', 'E']
Copy code
example 2: Create a with column names and a specific number of rows DataFrame
The following code shows how to create a with a specific column name and a specific number of rows pandas DataFrame.
import pandas as pd
#create DataFrame
df = pd.DataFrame(columns=['A', 'B', 'C', 'D', 'E'],
index=range(1, 10))
#view DataFrame
df
A B C D E
1 NaN NaN NaN NaN NaN
2 NaN NaN NaN NaN NaN
3 NaN NaN NaN NaN NaN
4 NaN NaN NaN NaN NaN
5 NaN NaN NaN NaN NaN
6 NaN NaN NaN NaN NaN
7 NaN NaN NaN NaN NaN
8 NaN NaN NaN NaN NaN
9 NaN NaN NaN NaN NaN
Copy code
Please note that ,DataFrame Each value in is filled with a NaN value .
Once again, , We can use shape To obtain a DataFrame Size .
#display shape of DataFrame
df.shape
(9, 5)
Copy code
This tells us ,DataFrame Yes 9 That's ok 5 Column .
Other resources
The following tutorial explains how to pandas Perform other common operations in .
How to be in Pandas Create a new column according to the condition in
How to be in Pandas Insert a column into the data frame
How to be in Pandas Set column as index in
The postPandas: How to Create Empty DataFrame with Column Namesappeared first onStatology.
copyright notice
author[Stone sword],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201301156299459.html
The sidebar is recommended
- Some people say Python does not support function overloading?
- "Python instance" was shocked and realized the dirty words and advertisement detection of the chat system with Python
- Introduction to Python - CONDA common commands
- Python actual combat | just "4 steps" to get started with web crawler (with benefits)
- Don't know what to eat every day? Python to tell you! Generate recipes and don't worry about what to eat every day!
- Are people who like drinking tea successful? I use Python to make a tea guide! Do you like it?
- I took 100g pictures offline overnight with Python just to prevent the website from disappearing
- Binary operation of Python OpenCV image re learning and image smoothing (convolution processing)
- Analysis of Python event mechanism
- Iterator of Python basic language
guess what you like
-
Base64 encryption and decryption in Python
-
Chapter 2: Fundamentals of python-2 variable
-
Python garbage collection summary
-
Python game development, pyGame module, python takes you to realize a magic tower game from scratch (1)
-
Python draws a spinning windmill with turtle
-
Deep understanding of Python features
-
A website full of temptations for Python crawler writers, "lovely picture network", look at the name of this website
-
Python opencv Canny edge detection knowledge supplement
-
Complex learning of Python opencv Sobel operator, ScHARR operator and Laplacian operator
-
Python: faker extension package
Random recommended
- Python code reading (Part 44): find the location of qualified elements
- Elegant implementation of Django model field encryption
- 40 Python entry applet
- Pandas comprehensive application
- Chapter 2: Fundamentals of python-3 character string
- Python pyplot draws a parallel histogram, and the x-axis value is displayed in the center of the two histograms
- [Python crawler] detailed explanation of selenium from introduction to actual combat [1]
- Curl to Python self use version
- Python visualization - 3D drawing solutions pyecharts, Matplotlib, openpyxl
- Use python, opencv's meanshift and CAMSHIFT algorithms to find and track objects in video
- Using python, opencv obtains and changes pixels, modifies image channels, and trims ROI
- [Python data collection] university ranking data collection
- [Python data collection] stock information collection
- Python game development, pyGame module, python takes you to realize a magic tower game from scratch (2)
- Python solves the problem of suspending execution after clicking the mouse in CMD window (fast editing mode is prohibited)
- [Python from introduction to mastery] (II) how to run Python? What are the good development tools (pycharm)
- Python type hints from introduction to practice
- Python notes (IX): basic operation of dictionary
- Python notes (8): basic operations of collections
- Python notes (VII): definition and use of tuples
- Python notes (6): definition and use of lists
- Python notes (V): string operation
- Python notes (IV): use of functions and modules
- Python notes (3): conditional statements and circular statements
- Python notes (II): lexical structure
- Notes on python (I): getting to know Python
- [Python data structure series] - tree and binary tree - basic knowledge - knowledge point explanation + code implementation
- [Python daily homework] Day7: how to combine two dictionaries in an expression?
- How to implement a custom list or dictionary in Python
- 15 advanced Python tips for experienced programmers
- Python string method tutorial - how to use the find() and replace() functions on Python strings
- Python computer network basics
- Python crawler series: crawling global airport information
- Python crawler series: crawling global port information
- How to calculate unique values using pandas groupby
- Application of built-in distribution of Monte Carlo simulation SciPy with Python
- Gradient lifting method and its implementation in Python
- Pandas: how to group and calculate by index
- Can you create an empty pandas data frame and fill it in?
- Python basic exercises teaching! can't? (practice makes perfect)