简体   繁体   English

根据列名称替换pandas数据框中的值

[英]Replace values in pandas dataframe based on column names

I would like to replace the values in a pandas dataframe from another series based on the column names. 我想根据列名替换另一个系列的pandas数据框中的值。 I have the foll. 我有傻瓜。 dataframe: 数据帧:

Y2000   Y2001   Y2002    Y2003    Y2004    Item    Item Code
34        43      0      0          25     Test      Val

and I have another series: 我还有另一个系列:

Y2000    41403766
Y2001    45283735
Y2002    47850796
Y2003    38639101
Y2004    45226813

How do I replace the values in the first dataframe based on the values in the 2nd series? 如何根据第二个系列中的值替换第一个数据框中的值?

--MORE EDITS: To recreate the proble, code and data is here: umd.box.com/s/hqd6oopj6vvp4qvpwnj8r4lm3z7as4i3 -更多编辑:要重新创建问题,代码和数据在这里: umd.box.com/s/hqd6oopj6vvp4qvpwnj8r4lm3z7as4i3

Instructions to run teh code: 运行代码的说明:

To run this code: 要运行此代码:

  1. Replace data_dir in config_rotations.txt with the path to the input directory ie where the files are kept 将config_rotations.txt中的data_dir替换为输入目录的路径,即保存文件的位置

  2. Replace out_dir in config_rotations.txt with whatever output path you want 用所需的任何输出路径替换config_rotations.txt中的out_dir

  3. Run python code\\crop_stats.py. 运行python code \\ crop_stats.py。 The problem is in line 133 of crop_stats.py 问题出在crop_stats.py的第133行

--EDIT: - 编辑:

Based on @Andy's query, here's the result I want: 基于@Andy的查询,这是我想要的结果:

Y2000      Y2001   Y2002     Y2003      Y2004          Item    Item Code
41403766  45283735 47850796  38639101  45226813     Test      Val

I tried 我试过了

df_a.replace(df_b)

but this does not change any value in df_a 但这不会更改df_a中的任何值

You can construct a df from the series after reshaping and overwrite the columns: 您可以在重塑和覆盖列之后从该系列构造一个df:

In [85]:
df1[s.index] = pd.DataFrame(columns = s.index, data = s.values.reshape(1,5))
df1

Out[85]:
      Y2000     Y2001     Y2002     Y2003     Y2004  Item Item  Code
0  41403766  45283735  47850796  38639101  45226813  Test        Val

So this uses the series index values to sub-select from the df and then constructs a df from the same series, here we have to reshape the array to make a single row df 因此,这使用序列索引值从df中进行子选择,然后从同一序列构造df,这里我们必须对数组进行整形以形成单行df

EDIT 编辑

The reason my code above won't work on your real code is firstly when assigning you can't do this: 我上面的代码无法在您的真实代码上运行的原因首先是在分配您无法执行此操作时:

df.loc[(df['Country Code'] == replace_cnt) & (df['Item'] == crop)][s.index]

This is called chained indexing and raises a warning, see the docs . 这称为链式索引并发出警告,请参阅docs

So to correct this you can put the columns inside the [] : 因此,要纠正此问题,您可以将列放在[]

df.loc[(df['Country Code'] == replace_cnt) & (df['Item'] == crop),s.index]

Additionally pandas tries to align along index values and column names, if they don't match then you'll get NaN values so you can get around this by calling .values to get a np array which just becomes anonymous data that has no index or column labels, so long as the data shape is broadcast-able then it will do what you want: 另外,pandas尝试沿索引值和列名对齐,如果它们不匹配,那么您将获得NaN值,因此您可以通过调用.values获得一个np数组来解决这个问题,该数组只是成为没有索引或列标签,只要数据形状是可广播的,它就会做您想要的:

df.loc[(df['Country Code'] == replace_cnt) & (df['Item'] == crop),s.index] = pd.DataFrame(columns=s.index, data=s.values.reshape(1, len(s.index))).values

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

相关问题 根据另一列的值替换Pandas数据框的Column的值 - Replace values of a Pandas dataframe's Column based on values of another column 根据与其他列名称匹配的列值填充 Pandas Dataframe - Populate Pandas Dataframe Based on Column Values Matching Other Column Names 如何对行值进行排序并将其替换为熊猫数据框上的列名 - How to sort rows values and replace them by column names on a pandas dataframe 如何根据字典键和值替换熊猫数据框列值? - How to replace pandas dataframe column values based on dictionary key and values? pandas Dataframe基于键列,将NaN值替换为以前的值 - pandas Dataframe Replace NaN values with with previous value based on a key column 根据条件用不同的替换字典替换熊猫数据框列中的值 - Replace values in pandas dataframe column with different replacement dict based on condition Pandas DataFrame:根据条件替换列中的所有值 - Pandas DataFrame: replace all values in a column, based on condition 根据条件,用相应的列名替换 pandas 数据框中的特定值, - Replace specific values in pandas dataframe with the corresponding column name, based on a condition, 根据包含的字典键替换 Pandas DataFrame 列值 - Replace Pandas DataFrame column values based on containing dictionary keys 根据 pandas dataframe 中的相邻列将 NaN 值替换为特定文本 - Replace NaN values with specific text based on adjacent column in pandas dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM