In pandas.DataFrame, even when assigning only a specific column, if index is attached, you do not have to worry about the order of data

What I found

When assigning a DataFrame type value to a DataFrame in column units, even if the data order is different, if the indexes match, you can assign as it is. It may be useful to know when extracting some columns from the DataFrame, processing them according to the conditions, and returning them.

Tried environment

Code example

Prepare modules and data

import numpy as np
import pandas as pd

# 0-19 sequence of 4*Formatted to 5 and converted to dataframe
df = pd.DataFrame(np.arange(20).reshape((4,5)), columns = list("abcde"))

#     a   b   c   d   e
# 0   0   1   2   3   4
# 1   5   6   7   8   9
# 2  10  11  12  13  14
# 3  15  16  17  18  19

Create a DataFrame with some columns extracted and process the values

df_e_1 = df.loc[[0,2], ["e"]]
#     e
# 0   4
# 2  14

df_e_1["e"] += 300
#      e
# 0  304
# 2  314

Create a DataFrame with some columns extracted and combine it with the data processed above.

df_e_2 = df.loc[[1,3], ["e"]]
#     e
# 1   9
# 3  19

df_e = pd.concat([df_e_1, df_e_2])
#      e
# 0  304
# 2  314
# 1    9
# 3   19

I thought that if df_e was not sorted in index order here, the data order would be out of order even if it was assigned to the original DataFrame ... Even if you substitute it as it is, it will match the index and automatically sort and substitute. Please pay attention to the e column below

df["e"] = df_e
print(df)
#     a   b   c   d    e
# 0   0   1   2   3  304
# 1   5   6   7   8    9
# 2  10  11  12  13  314
# 3  15  16  17  18   19

Recommended Posts

In pandas.DataFrame, even when assigning only a specific column, if index is attached, you do not have to worry about the order of data
What to do if you get the error RuntimeError: Python is not installed as a framework when trying to use matplitlib and pylab in Python 3.3
What to do if the progress bar is not displayed in tqdm of python
What to do if you get `locale.Error: unsupported locale setting` when getting the day of the week from a date in Python
Do not change the order of columns when concatenating pandas data frames.
What to do if you get a memory error when converting from PySparkDataFrame to PandasDataFrame
What to do when only the window is displayed and nothing is displayed in pygame Note
How to check in Python if one of the elements of a list is in another list
To extract the data of a specific column in a specific sheet in multiple Excel files at once and put the data in each column in one row
What to do if the library doesn't load when you run it in PyCharm, even though it works fine on terminal
What to do if you have corrected the mistake in the IP address of the zone file but cannot connect to the DNS server
Make a note of what you want to do in the future with Raspberry Pi
Get the value of a specific key up to the specified index in the dictionary list in Python
What to do if the image is not displayed using matplotlib etc. in the Docker container
What to do when a Missing artifact occurs in a jar that is not defined in pom.xml
What you should not do in the process of time series data analysis (including reflection)
When a character string of a certain series is in the Key of the dictionary, the character string is converted to the Value of the dictionary.
[Python] Extracts data frames that do not match a specific column with other data frames of Pandas
Be careful when assigning Series as a column to pandas.DataFrame
What to do if you cat or tail a binary file and the terminal is garbled
Write a script in Shell and Python to notify you in Slack when the process is finished
[Django] What to do if the model you want to create has a large number of fields
What to do when a part of the background image becomes transparent when the transparent image is combined with Pillow
What to do if you get a must override `get_config` error when trying to model.save in Keras
A story about what to do when a bad interpreter: Not such file or directory appears in Anaconda3 and how to investigate the cause.
What to do if you get a minus zero in Python
What to do if you can't use the trash in Lubuntu 18.04.
[Golang] Check if a specific character string is included in the character string
What to do when the value type is ambiguous in Python?
What to do if you get "The session could not be opened" when installing CentOS on VirtualBox
What to do if you get a "Wrong Python Platform" warning when using Python with the NetBeans IDE
If you want to switch the execution user in the middle of a Fabric task, settings context manager
[Python] What to do when PEP8 is violated in the process of importing from the directory added to sys.path
[Python] What to do if you get a ModuleNotFoundError when importing pandas using Jupyter Notebook in Anaconda