根据另一个df中的值填充新的pandas df

时间:2020-05-29 10:15:19

标签: python pandas loops dataframe if-statement

我在这里是新来的,所以请不要对我施加压力! :)

请参见下面的图片!

Print screen: df['Datan]

我正在尝试根据df['New_df']中的值创建一个新的数据帧(df['Datan']),以使df['New_df']等于字符串{的行上的df['Datan'] {1}}出现。如果该字符串不在#SRU中,则我希望df['Datan']“保留”上一行的值(其中包含df['New_df']字符串)。

请参阅下面df的说明。

#SRU

我一直在尝试for循环,特别是使用apply方法将if语句与if语句结合使用,但到目前为止尚未找到解决方案。在此处的任何其他线程中都找不到此特定问题。

2 个答案:

答案 0 :(得分:2)

使用Series.str.containsSeries.maskSeries.ffill的组合:

executeJwtAuthenticationService(username, password) {
  const timeoutPromise = new Promise(resolve => {
    const timeout = setTimeout(() => {
      clearTimeout(timeout);
      resolve('It took too long...');
    }, 8000);
  });

  const request = axios.post(`${API_URL_LOCAL}`, {
    username,
    password,
  }); 

  return Promise.race([timeoutPromise, request]);
}

结果:

m = df['Datan'].str.contains(r'#SRU')
df['New_df'] = df['Datan'].mask(~m).ffill()

答案 1 :(得分:2)

使用str.contains检查mvn clean package mvn package appengine:deploy -Dapp.deploy.projectId=myProjectIdHere nohup mvn exec:exec -Dfirestore.project.id="myProjectIdHere" &> program.out & 匹配,然后使用ffill填写string

na
df['New_df'] = df.Datan.where(df.Datan.str.contains('#SRU')).ffill()