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.
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