I would like to replace the values in a pandas dataframe from another series based on the column names. 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
Instructions to run teh code:
To run this code:
Replace data_dir in config_rotations.txt with the path to the input directory ie where the files are kept
Replace out_dir in config_rotations.txt with whatever output path you want
Run python code\\crop_stats.py. The problem is in line 133 of crop_stats.py
--EDIT:
Based on @Andy's query, here's the result I want:
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
You can construct a df from the series after reshaping and overwrite the columns:
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
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 .
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:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.