简体   繁体   English

如果索引在另一个数据框中而不列出列,熊猫会替换所有列值吗?

[英]Pandas replace all column values if index is in another dataframe without list the columns?

I have two dataframes ( df1, df3 ) with 12 columns and values for the next 12 months.我有两个数据框( df1, df3 ),其中包含 12 列和接下来 12 个月的值。 I want to replace the values of df1 if index is in df2 with values of df3 .如果索引在df2 ,我想用df3的值替换df1值。 The two dataframes ( df1, df3 ) comes automatically with 12-month rolling columns.两个数据框 ( df1, df3 ) 自动带有 12 个月滚动列。

df1 = pd.DataFrame(np.array([[100, 200, 300, 100, 150, 200, 220, 230, 300, 340, 250, 300], [400, 500, 600, 200, 300, 100, 150, 200, 220, 230, 300, 340], [150, 200, 220, 230, 300, 340, 200, 150, 90, 200, 200, 150]]),
               columns=['March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December', 'January', 'February'], 
               index= ['AB', 'CD', 'EF'])


df3 = pd.DataFrame(np.array([[200, 120, 150, 200, 300, 150, 150, 200, 320, 230, 250, 120], [450, 400, 500, 100, 300, 200, 150, 200, 320, 230, 250, 100], [300, 250, 190, 300, 200, 150, 200, 120, 150, 200, 300, 100]]),
               columns=['March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December', 'January', 'February'], 
               index= ['AB', 'CD', 'EF'])


data = ['AB', 'RE', 'AF'] 
df2 = pd.DataFrame(data, columns = ['Lookup'])

Expected output would be for index 'AB' the values of df3 for the next 12 months and for 'CD' and 'EF' the values of df1 .预期输出将是索引 'AB' 未来 12 个月的df3值,以及 'CD' 和 'EF' df1的值。

After trying and google I came across the following code which perfectly works.在尝试和谷歌之后,我遇到了以下完美工作的代码。

matches = df1.index.isin(df2.Lookup)
df1.loc[matches, :] = df3.loc[matches, :]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用熊猫数据框中另一列中的值替换空列表 - replace empty list with values in another column in pandas dataframe 用一列替换pandas数据框中的所有列 - Replace all columns in pandas dataframe with one column 如果熊猫数据框列中存在列表值列表,请用另一个熊猫列中的值替换它们 - If list of lists values are present in Pandas dataframe column replace them with values from another Pandas column 按熊猫数据框的索引对所有列的值进行分组 - Grouping the values of all columns by index of a pandas dataframe 将 Pandas Dataframe 中的行按索引替换为另一个 Dataframe 中具有相应索引的值 - Replace rows by index in a Pandas Dataframe with values with corresponding index in another Dataframe 如果另一个 dataframe 中没有具有相同索引和列名的值,如何清除 pandas 中 dataframe 的列中的值 - How to clear values in columns of a dataframe in pandas if there are no values in another dataframe with the same index an column names Pandas 将列的值替换为与另一个 Dataframe 的比较 - Pandas replace values of a column with comparison to another Dataframe Pandas:通过将 2 列值(复合键)匹配到 1 列和另一个 dataframe 的列标签/索引来映射 2 个数据帧之间的值 - Pandas: Mapping values between 2 dataframes by matching 2 columns values (composite key) to 1 column and the column labels/index of another dataframe 如何替换不在列表中的Pandas Dataframe中的所有值? - How to replace all values in a Pandas Dataframe not in a list? 用另一个 DataFrame 替换 pandas 多索引 DataFrame 的列 - Replace column of pandas multi-index DataFrame with another DataFrame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM