Pet*_*rov 2 python dataframe pandas
我有2个数据帧
category count_sec_target
3D-?????? 0.09375
C?????? 201.90625
GPS ? ??????? 0.015625
Hi-Tech 187.1484375
???????????? 0.8125
???????????? 8.40625
Run Code Online (Sandbox Code Playgroud)
和
category count_sec_random
3D-?????? 0.369565217
Hi-Tech 70.42391304
??? ??, ??????????????? 0.934782609
???????????? 1.413043478
???????????? 14.93478261
???? 480.3369565
Run Code Online (Sandbox Code Playgroud)
我需要连接并得到
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
Run Code Online (Sandbox Code Playgroud)
接下来我想在col中划分值(count_sec_target / count_sec_random) * 100%
但是当我尝试连接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
Run Code Online (Sandbox Code Playgroud)
另外我试试df = df1.append(df2)
BUt我得错了结果.我该如何解决这个问题?
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
Run Code Online (Sandbox Code Playgroud)
设置参考
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]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
149 次 |
| 最近记录: |