简体   繁体   English

为什么不能分配多个与原始 pandas df 中名称不同的列?

[英]Why can't you assign multiple columns that are not the same name as in the original pandas df?

     odds_ft_home_team_win  odds_ft_draw  odds_ft_away_team_win
0                 0.850212      0.100281               0.049506
1                 0.081114      0.146371               0.772515
2                 0.486790      0.266734               0.246476
3                 0.355737      0.301008               0.343255
4                 0.294952      0.299490               0.405559

This is a dataframe prob_odds I want to assign to my previous data-frame df .这是一个 dataframe prob_odds我想分配给我以前的数据帧df

df.loc[:,["w","d","a"]]=prob_odds

As you can see I want the new columns to have a different name than in the original dataframe.如您所见,我希望新列的名称与原始 dataframe 中的名称不同。 This returns a data-frame that has the columns "w","d","a" with NA values.这将返回一个数据框,其中包含具有 NA 值"w","d","a"列。 If I change the names of the columns in the prob_odds Data-Frame to match the names in the df the assignment works.如果我更改prob_odds数据帧中列的名称以匹配df中的名称,则分配有效。 Why is that?这是为什么? What is the way to go around this so that you can assign new columns without having to rename the old ones beforehand. go 的方法是什么,这样您就可以分配新列而不必事先重命名旧列。

You can, just without .loc (because .loc would search for existing indexes and columns to operate on):你可以,只是没有.loc (因为.loc会搜索现有的索引和列进行操作):

N = 10
df = pd.DataFrame({'a': 1 * np.ones(N), 'b': 2 * np.ones(N)})
df[['c', 'd']] = pd.DataFrame({'x': 3 * np.ones(N), 'y': 4 * np.ones(N)})

df

Output: Output:

     a    b    c    d
0  1.0  2.0  3.0  4.0
1  1.0  2.0  3.0  4.0
2  1.0  2.0  3.0  4.0
3  1.0  2.0  3.0  4.0
4  1.0  2.0  3.0  4.0
5  1.0  2.0  3.0  4.0
6  1.0  2.0  3.0  4.0
7  1.0  2.0  3.0  4.0
8  1.0  2.0  3.0  4.0
9  1.0  2.0  3.0  4.0

PS To match your naming, df[["w","d","a"]] = prob_odds should work PS为了匹配您的命名, df[["w","d","a"]] = prob_odds应该可以工作

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

相关问题 将 Pandas DF 中的所有列转换为单独的列表,并为其分配与列名称相同的名称 - Convert all columns in a Pandas DF to separate lists and assign it a name same as column name 将具有相同名称但不同编号的多个列复制到 python/pandas 中的新 df - copy multiple columns with same name but different number into new df in python/pandas 熊猫数据框:是否可以为列名和/或df值分配标签? - Pandas dataframe: Can you assign a label for the column names and/or the df values? Pandas 中的多个同名列 - Multiple columns with the same name in Pandas Python,Pandas数据框:为什么我不能直接将数据分配给df.values = df.values / 100? - Python, Pandas Dataframes: Why can't I assign data directly to the df.values=df.values/100? 熊猫,为什么我不能用 df.loc[condition, 'col'] = df.loc[condition, 'col'].modification 赋值? - Pandas, why can't I assign with df.loc[condition, 'col'] = df.loc[condition, 'col'].modification? 在多列熊猫df上划分 - Division on multiple columns pandas df 为什么你能在熊猫中做df.loc(False)['value']? - Why can you do df.loc(False)['value'] in pandas? 将值分配给具有相同行相同值的多行熊猫列 - Assign values to Multiple rows pandas columns with same rows same value 强制熊猫保持多个具有相同名称的列 - Force Pandas to keep multiple columns with the same name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM