How to join Panda DataFrames based on List values in a column [duplicate]











up vote
0
down vote

favorite













This question already has an answer here:




  • How do I unnest a column in a pandas DataFrame?

    3 answers




There are two Pandas DataFrame



df_A = pd.DataFrame([['r1', ['a','b']], ['r2',['aabb','b']], ['r3', ['xyz']]], columns=['col1', 'col2'])

col1 col2
r1 [a, b]
r2 [aabb, b]
r3 [xyz]


df_B = pd.DataFrame([['a', 10], ['b',2]], columns=['C1', 'C2'])

C1 C2
a 10
b 2


I want to join both dataframes such as df_C is



col1 C1  C2
r1 a 10
r1 b 2
r2 aabb 0
r2 b 2
r3 xyz 0









share|improve this question













marked as duplicate by Sandeep Kadapa, coldspeed pandas
Users with the  pandas badge can single-handedly close pandas questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
17 hours ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Thanks, I guess, it might need expert-level knowledge to understand "unsetting" is the same as what I was looking for.
    – Watt
    5 hours ago

















up vote
0
down vote

favorite













This question already has an answer here:




  • How do I unnest a column in a pandas DataFrame?

    3 answers




There are two Pandas DataFrame



df_A = pd.DataFrame([['r1', ['a','b']], ['r2',['aabb','b']], ['r3', ['xyz']]], columns=['col1', 'col2'])

col1 col2
r1 [a, b]
r2 [aabb, b]
r3 [xyz]


df_B = pd.DataFrame([['a', 10], ['b',2]], columns=['C1', 'C2'])

C1 C2
a 10
b 2


I want to join both dataframes such as df_C is



col1 C1  C2
r1 a 10
r1 b 2
r2 aabb 0
r2 b 2
r3 xyz 0









share|improve this question













marked as duplicate by Sandeep Kadapa, coldspeed pandas
Users with the  pandas badge can single-handedly close pandas questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
17 hours ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Thanks, I guess, it might need expert-level knowledge to understand "unsetting" is the same as what I was looking for.
    – Watt
    5 hours ago















up vote
0
down vote

favorite









up vote
0
down vote

favorite












This question already has an answer here:




  • How do I unnest a column in a pandas DataFrame?

    3 answers




There are two Pandas DataFrame



df_A = pd.DataFrame([['r1', ['a','b']], ['r2',['aabb','b']], ['r3', ['xyz']]], columns=['col1', 'col2'])

col1 col2
r1 [a, b]
r2 [aabb, b]
r3 [xyz]


df_B = pd.DataFrame([['a', 10], ['b',2]], columns=['C1', 'C2'])

C1 C2
a 10
b 2


I want to join both dataframes such as df_C is



col1 C1  C2
r1 a 10
r1 b 2
r2 aabb 0
r2 b 2
r3 xyz 0









share|improve this question














This question already has an answer here:




  • How do I unnest a column in a pandas DataFrame?

    3 answers




There are two Pandas DataFrame



df_A = pd.DataFrame([['r1', ['a','b']], ['r2',['aabb','b']], ['r3', ['xyz']]], columns=['col1', 'col2'])

col1 col2
r1 [a, b]
r2 [aabb, b]
r3 [xyz]


df_B = pd.DataFrame([['a', 10], ['b',2]], columns=['C1', 'C2'])

C1 C2
a 10
b 2


I want to join both dataframes such as df_C is



col1 C1  C2
r1 a 10
r1 b 2
r2 aabb 0
r2 b 2
r3 xyz 0




This question already has an answer here:




  • How do I unnest a column in a pandas DataFrame?

    3 answers








python pandas






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 18 hours ago









Watt

1,59184165




1,59184165




marked as duplicate by Sandeep Kadapa, coldspeed pandas
Users with the  pandas badge can single-handedly close pandas questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
17 hours ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Sandeep Kadapa, coldspeed pandas
Users with the  pandas badge can single-handedly close pandas questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
17 hours ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • Thanks, I guess, it might need expert-level knowledge to understand "unsetting" is the same as what I was looking for.
    – Watt
    5 hours ago




















  • Thanks, I guess, it might need expert-level knowledge to understand "unsetting" is the same as what I was looking for.
    – Watt
    5 hours ago


















Thanks, I guess, it might need expert-level knowledge to understand "unsetting" is the same as what I was looking for.
– Watt
5 hours ago






Thanks, I guess, it might need expert-level knowledge to understand "unsetting" is the same as what I was looking for.
– Watt
5 hours ago














1 Answer
1






active

oldest

votes

















up vote
1
down vote













You need:



df = pd.DataFrame([['r1', ['a','b']], ['r2',['aabb','b']], ['r3', ['xyz']]], columns=['col1', 'col2'])

df= pd.DataFrame({'col1':np.repeat(df.col1.values, df.col2.str.len()),
'C1':np.concatenate(df.col2.values)})

df_B = pd.DataFrame([['a', 10], ['b',2]], columns=['C1', 'C2'])
df_B = dict(zip(df_B.C1, df_B.C2))
# {'a': 10, 'b': 2}

df['C2']= df['C1'].apply(lambda x: df_B[x] if x in df_B.keys() else 0)

print(df)


Output:



    col1  C1    C2
0 r1 a 10
1 r1 b 2
2 r2 aabb 0
3 r2 b 2
4 r3 xyz 0





share|improve this answer





















  • Thanks, can you please explain what you are doing here df= pd.DataFrame({'col1':np.repeat(df.col1.values, df.col2.str.len()), 'C1':np.concatenate(df.col2.values)})
    – Watt
    5 hours ago


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote













You need:



df = pd.DataFrame([['r1', ['a','b']], ['r2',['aabb','b']], ['r3', ['xyz']]], columns=['col1', 'col2'])

df= pd.DataFrame({'col1':np.repeat(df.col1.values, df.col2.str.len()),
'C1':np.concatenate(df.col2.values)})

df_B = pd.DataFrame([['a', 10], ['b',2]], columns=['C1', 'C2'])
df_B = dict(zip(df_B.C1, df_B.C2))
# {'a': 10, 'b': 2}

df['C2']= df['C1'].apply(lambda x: df_B[x] if x in df_B.keys() else 0)

print(df)


Output:



    col1  C1    C2
0 r1 a 10
1 r1 b 2
2 r2 aabb 0
3 r2 b 2
4 r3 xyz 0





share|improve this answer





















  • Thanks, can you please explain what you are doing here df= pd.DataFrame({'col1':np.repeat(df.col1.values, df.col2.str.len()), 'C1':np.concatenate(df.col2.values)})
    – Watt
    5 hours ago















up vote
1
down vote













You need:



df = pd.DataFrame([['r1', ['a','b']], ['r2',['aabb','b']], ['r3', ['xyz']]], columns=['col1', 'col2'])

df= pd.DataFrame({'col1':np.repeat(df.col1.values, df.col2.str.len()),
'C1':np.concatenate(df.col2.values)})

df_B = pd.DataFrame([['a', 10], ['b',2]], columns=['C1', 'C2'])
df_B = dict(zip(df_B.C1, df_B.C2))
# {'a': 10, 'b': 2}

df['C2']= df['C1'].apply(lambda x: df_B[x] if x in df_B.keys() else 0)

print(df)


Output:



    col1  C1    C2
0 r1 a 10
1 r1 b 2
2 r2 aabb 0
3 r2 b 2
4 r3 xyz 0





share|improve this answer





















  • Thanks, can you please explain what you are doing here df= pd.DataFrame({'col1':np.repeat(df.col1.values, df.col2.str.len()), 'C1':np.concatenate(df.col2.values)})
    – Watt
    5 hours ago













up vote
1
down vote










up vote
1
down vote









You need:



df = pd.DataFrame([['r1', ['a','b']], ['r2',['aabb','b']], ['r3', ['xyz']]], columns=['col1', 'col2'])

df= pd.DataFrame({'col1':np.repeat(df.col1.values, df.col2.str.len()),
'C1':np.concatenate(df.col2.values)})

df_B = pd.DataFrame([['a', 10], ['b',2]], columns=['C1', 'C2'])
df_B = dict(zip(df_B.C1, df_B.C2))
# {'a': 10, 'b': 2}

df['C2']= df['C1'].apply(lambda x: df_B[x] if x in df_B.keys() else 0)

print(df)


Output:



    col1  C1    C2
0 r1 a 10
1 r1 b 2
2 r2 aabb 0
3 r2 b 2
4 r3 xyz 0





share|improve this answer












You need:



df = pd.DataFrame([['r1', ['a','b']], ['r2',['aabb','b']], ['r3', ['xyz']]], columns=['col1', 'col2'])

df= pd.DataFrame({'col1':np.repeat(df.col1.values, df.col2.str.len()),
'C1':np.concatenate(df.col2.values)})

df_B = pd.DataFrame([['a', 10], ['b',2]], columns=['C1', 'C2'])
df_B = dict(zip(df_B.C1, df_B.C2))
# {'a': 10, 'b': 2}

df['C2']= df['C1'].apply(lambda x: df_B[x] if x in df_B.keys() else 0)

print(df)


Output:



    col1  C1    C2
0 r1 a 10
1 r1 b 2
2 r2 aabb 0
3 r2 b 2
4 r3 xyz 0






share|improve this answer












share|improve this answer



share|improve this answer










answered 18 hours ago









Sociopath

2,92161331




2,92161331












  • Thanks, can you please explain what you are doing here df= pd.DataFrame({'col1':np.repeat(df.col1.values, df.col2.str.len()), 'C1':np.concatenate(df.col2.values)})
    – Watt
    5 hours ago


















  • Thanks, can you please explain what you are doing here df= pd.DataFrame({'col1':np.repeat(df.col1.values, df.col2.str.len()), 'C1':np.concatenate(df.col2.values)})
    – Watt
    5 hours ago
















Thanks, can you please explain what you are doing here df= pd.DataFrame({'col1':np.repeat(df.col1.values, df.col2.str.len()), 'C1':np.concatenate(df.col2.values)})
– Watt
5 hours ago




Thanks, can you please explain what you are doing here df= pd.DataFrame({'col1':np.repeat(df.col1.values, df.col2.str.len()), 'C1':np.concatenate(df.col2.values)})
– Watt
5 hours ago



Popular posts from this blog

Create new schema in PostgreSQL using DBeaver

Deepest pit of an array with Javascript: test on Codility

Costa Masnaga