简体   繁体   中英

Pandas: concatenate dataframes

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?

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

在此输入图像描述


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]

Merge should be appropriate here:

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

图片


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

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