根据数据框中另一列的值向列表中的数据框列分配值

时间:2020-02-07 19:31:42

标签: python pandas

基于数据框中另一列的值从列表中为数据框列分配值

我有两个数据帧df1和df2,其中

df1 = pd.DataFrame(np.array([['a', 'b', 'c','d'], [1, 2, 3, 4]]),
                   columns=['x', 'y'])


df2 = pd.DataFrame(np.array([['a', 'b', 'c', 'a', 'c', 'b','b'], [4, 5, 6, 1, 32, 1, 8]]),
                   columns=['x', 'z'])


我想基于df2创建一个数据帧df3,并为其分配适当的df1 ['y']值。例如,我希望结果看起来像这样:

df3 = pd.DataFrame(np.array([['a', 'b', 'c', 'a', 'c', 'b','b'], [4, 5, 6, 1, 32, 1, 8] , [1, 2, 3, 1, 3, 2, 2 ] ]),
                   columns=['x', 'z', 'y'])

在我的实际情况下,x可能有成千上万个值,因此我想尽可能避免使用if df3['x'] == a: df3['y'] = 1样式的解决方案。

2 个答案:

答案 0 :(得分:0)

您可以通过合并来实现:

import SwiftUI

struct ContentView: View {
    @State var x: Int = 5
    @State var y: Int = 5
    var body: some View {
        NavigationView {
            ScrollView([.horizontal, .vertical]) {
                VStack(spacing: 8) {
                    ForEach(0 ..< self.y, id: \.self) { yCoor in
                        HStack(spacing: 8) {
                            ForEach(0 ..< self.x, id: \.self) { xCoor in
                                RoundedRectangle(cornerRadius: 10)
                                    .frame(width: 120, height: 120)
                                    .foregroundColor(.orange)
                                    .overlay(Text("(\(xCoor), \(yCoor))"))
                            }
                        }
                    }
                }
                .border(Color.green)
            }
            .border(Color.blue)
            .navigationBarTitle("Contents of ScrollView misplaced", displayMode: .inline)
            .navigationBarItems(
                leading: HStack(spacing: 16) {
                    Text("x:")
                        .bold()
                    Button(action: { if self.x > 0 { self.x -= 1 } }) {
                        Image(systemName: "minus.circle.fill")
                            .imageScale(.large)
                    }
                    Button(action: { self.x += 1 }) {
                        Image(systemName: "plus.circle.fill")
                            .imageScale(.large)
                    }
                },
                trailing: HStack(spacing: 16) {
                    Text("y:")
                        .bold()
                    Button(action: { if self.y > 0 { self.y -= 1 } }) {
                        Image(systemName: "minus.circle.fill")
                            .imageScale(.large)
                    }
                    Button(action: { self.y += 1 }) {
                        Image(systemName: "plus.circle.fill")
                            .imageScale(.large)
                    }
                }
            )
        }
        .navigationViewStyle(StackNavigationViewStyle())
    }
}

答案 1 :(得分:0)

设置:

  async loadNewSong(playing) {
    if (this.activeAudio) {
      this.activeAudio.unloadAsync()
      this.activeAudio.setOnPlaybackStatusUpdate(null)
      null
    }

    const source = { uri: this.props.songList[this.state.activeSection].url }

    const initialStatus = {
      shouldPlay: playing,
      rate: this.state.rate,
      volume: this.state.volume
    }

    const { sound } = await Audio.Sound.createAsync(
      source,
      initialStatus,
      this.onPlaybackStatusUpdate
    )
    this.activeAudio = sound

  }

词典方法:

import pandas as pd
import numpy as np

data={'x':['a', 'b', 'c','d'],
      'y':[1, 2, 3, 4]}
df1 = pd.DataFrame(data)

data2={'x':['a', 'b', 'c', 'a', 'c', 'b','b'],
       'z':[4, 5, 6, 1, 32, 1, 8]}
df2 = pd.DataFrame(data2)

data3={'x':['a', 'b', 'c', 'a', 'c', 'b','b'],
       'z':[4, 5, 6, 1, 32, 1, 8]}
df3 = pd.DataFrame(data3)

您必须解压缩# Make a dictionary dict = df1.set_index('x').to_dict() # Map to dictionary df3 = df2.assign(x=df2['x'].map(*dict.values())) ,因为它实际上是2个字典,但是您只想在这里找到第二个。