简体   繁体   中英

python dictionary to create new data frame column

I created a dictionary consisting of unique values from two columns in two separate files. These two columns have equivalent values that are formatted differently and therefore will not merge correctly.

dataframe A:

A  B
1  dfg 
2  srg
3  sgf
4  sfh
3  srg
6  srg
1  sfg

d={1: 1.102832,
   2: 2.102832,
   3: 3.102832,
   4: 4.102832,
   5: 5.102832,
   6: 6.102832,
   7: 7.102832}

Final product should look like this:

A  B     C
1  dfg  1.102832
2  srg  2.102832
3  sgf  3.102832
4  sfh  4.102832
3  srg  3.102832
6  srg  6.102832
1  sfg  1.102832

I attempted to use pandas.Series.map as follows:

s["C"]=s["A"].map(dictionary)

Unfortunately my result looks like this:

A  B     C
1  dfg  NaN
2  srg  NaN
3  sgf  NaN
4  sfh  NaN
3  srg  NaN
6  srg  NaN
1  sfg  NaN

What am I missing?

for key,value in d.iteritems():
    A.ix[A['A']==key,'C'] = value 

Assuming your first dataframe is called 'A' en your dictionary is called 'd'

In theory what you tried should work. However, you have to pay careful attention to the types of the values in s['A'] and the keys in d lest they do not compare equally. For example, if s['A'] contains strings, while d.keys() contains ints, then

import pandas as pd
d = {1: 1.102832,
     2: 2.102832,
     3: 3.102832,
     4: 4.102832,
     5: 5.102832,
     6: 6.102832,
     7: 7.102832}

s = pd.DataFrame({
    'A':'1 2 3 4 3 6 1'.split(),
    'B':'dfg srg sgf sfh srg srg sfg'.split()})

s['C'] = s['A'].map(d)
print(s)

yields

   A    B   C
0  1  dfg NaN
1  2  srg NaN
2  3  sgf NaN
3  4  sfh NaN
4  3  srg NaN
5  6  srg NaN
6  1  sfg NaN

whereas if you convert s['A'] to numerical values:

s['A'] = pd.to_numeric(s['A'], errors='coerce')
s['C'] = s['A'].map(d)
print(s)

then you get the desired result:

   A    B         C
0  1  dfg  1.102832
1  2  srg  2.102832
2  3  sgf  3.102832
3  4  sfh  4.102832
4  3  srg  3.102832
5  6  srg  6.102832
6  1  sfg  1.102832

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM