【问题标题】:How to organize variables into dataframe but not populate value of variables per column of dataframe?如何将变量组织到数据框中,但不填充每列数据框的变量值?
【发布时间】:2018-03-20 09:42:42
【问题描述】:

我想将变量中的数据添加到数据框中。我遇到的问题是我需要组织变量,它们没有填充每一行。

我需要数据看起来像这样:

 name   notification1   notification2   notification3
    a          1        
    b                          2    
    c                                         3

数据框目前如下所示:

name    notification1   notification2   notification3
a          1                1             1 
b          2                2             2
c          3                3             3                                   

变量设置如下(所有变量都是str):

notification1 = 1.0
notification2 = 2.0
notification3 = 3.0
person_notification1 = a
person_notification2 = b
person_notification3 = c

每个通知都只附加一个人,因此并非每一行都需要每个人的数据。

提前谢谢你希望我的问题有意义。

【问题讨论】:

  • 您用于创建此数据框的代码在哪里?

标签: python pandas dataframe


【解决方案1】:

一种方法:

import pandas as pd

notification1 = 1.0
notification2 = 2.0
notification3 = 3.0
person_notification1 = 'a'
person_notification2 = 'b'
person_notification3 = 'c'

def row(name, notification):
    return {'name': name, 'notification_'+str(notification) : notification}

df = pd.DataFrame()
df = df.append(row(person_notification1, int(notification1)),ignore_index=True)
df = df.append(row(person_notification2, int(notification2)),ignore_index=True)
df = df.append(row(person_notification3, int(notification3)),ignore_index=True)

结果:

  name  notification_1  notification_2  notification_3
0    a             1.0             NaN             NaN
1    b             NaN             2.0             NaN
2    c             NaN             NaN             3.0

【讨论】:

    【解决方案2】:

    考虑将数据存储在列表中,而不是单个变量。

    notifs = [1, 2, 3]
    persons = ['a', 'b', 'c']
    

    使用np.diag初始化一个对角二维数组,并将其传递给pd.DataFrame

    pd.DataFrame(
         np.diag(notifs), 
         index=persons, 
         columns=np.arange(1, len(notifs) + 1)
    ).add_prefix('notification')
    
       notification1  notification2  notification3
    a              1              0              0
    b              0              2              0
    c              0              0              3
    

    【讨论】:

      【解决方案3】:

      我认为您希望每一列只有一个值,并将其作为空字符串。请在下面找到我的解决方案。希望对你有帮助。

      import pandas as pd
      import numpy as np
      
      def main():
          notification = [1.0, 2.0, 3.0]
          persons = ['a', 'b', 'c']
          columns  = ['notification{}'.format(i) for i, elem in enumerate(notification, 1)]
          df = pd.DataFrame(columns=columns)
      
          for r,c,v in zip(persons,columns,notification):
              df.at[r,c] = v
      
          df = df.replace(np.nan, '', regex=True)
          print(df)
      
      
      
      if __name__ == '__main__':
          main()
      

      输出

        notification1 notification2 notification3
      a             1                            
      b                           2              
      c                                         3
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-09-29
        • 2019-06-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多