2

I have 2 dataframe

category count_sec_target 3D-шутеры 0.09375 Cериалы 201.90625 GPS и ГЛОНАСС 0.015625 Hi-Tech 187.1484375 Абитуриентам 0.8125 Авиакомпании 8.40625 

and

category count_sec_random 3D-шутеры 0.369565217 Hi-Tech 70.42391304 АСУ ТП, промэлектроника 0.934782609 Абитуриентам 1.413043478 Авиакомпании 14.93478261 Авто 480.3369565 

I need to concatenate that And get

category count_sec_target count_sec_random 3D-шутеры 0.09375 0.369565217 Cериалы 201.90625 0 GPS и ГЛОНАСС 0.015625 0 Hi-Tech 187.1484375 70.42391304 Абитуриентам 0.8125 1.413043478 Авиакомпании 8.40625 14.93478261 АСУ ТП, промэлектроника 0 0.934782609 Авто 0 480.3369565 

And next I want to divide values in col (count_sec_target / count_sec_random) * 100% But when I try to concatenate df

frames = [df1, df1] df = pd.concat(frames) I get category count_sec_random count_sec_target 0 3D-шутеры 0.369565 NaN 1 Hi-Tech 70.423913 NaN 2 АСУ ТП, промэлектроника 0.934783 NaN 3 Абитуриентам 1.413043 NaN 4 Авиакомпании 14.934783 NaN 

Also I try df = df1.append(df2) BUt I get wrong result. How can I fix that?

1
  • Maybe you need to use pd.merge in order to get your desired output Commented Sep 21, 2016 at 9:59

2 Answers 2

5
df3 = pd.concat([d.set_index('category') for d in frames], axis=1).fillna(0) df3['ratio'] = df3.count_sec_random / df3.count_sec_target df3 

enter image description here


Setup Reference

import pandas as pd from StringIO import StringIO t1 = """category;count_sec_target 3D-шутеры;0.09375 Cериалы;201.90625 GPS и ГЛОНАСС;0.015625 Hi-Tech;187.1484375 Абитуриентам;0.8125 Авиакомпании;8.40625""" t2 = """category;count_sec_random 3D-шутеры;0.369565217 Hi-Tech;70.42391304 АСУ ТП, промэлектроника;0.934782609 Абитуриентам;1.413043478 Авиакомпании;14.93478261 Авто;480.3369565""" df1 = pd.read_csv(StringIO(t1), sep=';') df2 = pd.read_csv(StringIO(t2), sep=';') frames = [df1, df2] 
Sign up to request clarification or add additional context in comments.

2 Comments

Can you say, how can I next divide one column to other?
and I need to get column index at the left
3

Merge should be appropriate here:

df_1.merge(df_2, on='category', how='outer').fillna(0) 

Image


To get the division output, simply do:

df['division'] = df['count_sec_target'].div(df['count_sec_random']) * 100 

where: df is the merged DF

1 Comment

I was just going to add this ;-) Agreed, this is more appropriate.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.