如何将依赖列追加到行中?

问题描述 投票:0回答:1

重现数据的代码是

 data = {
    'datetime': ['2019-01-01 08:03:00', '2019-01-01 08:04:00', '2019-01-01 08:04:00'],
    'own_mmsi': [236385000, 236385000, 244013009],
    'target_mmsi': [244013009, 244013009, 236385000],
    'own_lat': [59.330711, 59.330400, 59.327970],
    'own_lon': [10.515833, 10.514336, 10.515833],
    'target_lat': [59.327478, 59.327970, 59.330400],
    'target_lon': [10.515475, 10.515833, 10.514336],
    
}

我想将 target_lat 和 target_lon 放入 lat lon 列,将 own_mmsi 和 target_mmsi 放入 mmsi 列,它们取决于整个 df 的每一行的日期时间。

这是所需的输出前 2 行。

日期时间 mmsi 纬度
2019-01-01 08:03:00 236385000 59.330711 10.515833
2019-01-01 08:03:00 244013009 59.327478 10.515475
2019-01-01 08:04:00 236385000 59.330400 10.514336

我尝试将 df 与熔化和连接逻辑合并,但合并在相同的 df 中不起作用。

melted_df = pd.melt(
    df,
    id_vars=['datetime', 'own_mmsi'],
    value_vars=['target_mmsi', 'target_lat', 'target_lon'],
    var_name='attribute',
    value_name='value'
)

reshaped_df = melted_df.pivot_table(
    index=['datetime', 'own_mmsi'],
    columns='attribute',
    values='value',
    aggfunc='first'
).reset_index()
   
reshaped_df.columns.name = None  # Remove the name of the columns index
reshaped_df = reshaped_df.rename(columns={'target_mmsi': 'own_target_mmsi', 'target_lat': 'own_target_lat', 'target_lon': 'own_target_lon'})

print(reshaped_df)
python pandas dataframe numpy group-by
1个回答
0
投票

IIUC,您正在寻找

pd.wide_to_long

df.columns = df.columns.str.split('_').map(lambda x : '_'.join(x[::-1]))
out = (pd.wide_to_long(df.reset_index(), stubnames=['mmsi', 'lat', 'lon'],
                                         i=['index', 'datetime'], j='var',
                                         sep='_', suffix='\w+')
         .reset_index('datetime').reset_index(drop=True))

输出:

>>> out
              datetime       mmsi        lat        lon
0  2019-01-01 08:03:00  236385000  59.330711  10.515833
1  2019-01-01 08:03:00  244013009  59.327478  10.515475
2  2019-01-01 08:04:00  236385000  59.330400  10.514336
3  2019-01-01 08:04:00  244013009  59.327970  10.515833
4  2019-01-01 08:04:00  244013009  59.327970  10.515833
5  2019-01-01 08:04:00  236385000  59.330400  10.514336
© www.soinside.com 2019 - 2024. All rights reserved.