【问题标题】:To select particular row and column in DataFrame在 DataFrame 中选择特定的行和列
【发布时间】:2019-01-15 08:40:15
【问题描述】:

我的要求有点复杂

下面给出的是 DataFrame df:

                       Month1 Month2   Month3   Month4 Month5  Month6
Credit               4644.5  11142  6198.33  2830.48   5886  8381.5
No. of transactions       8      4        6       14      6       4

所以我想要的是,我想将所有这些值保存在一个单独的变量中。

例如,最终结果应该是:

CreditMonth1 = 4644.5
CreditMonth2 = 11142 
...
so on.. and
No. of transactionsMonth1 = 8
No. of transactionsMonth2 = 4
...
so on..

上面的结果可以用下面的代码来实现:

CreditMonth1= df.at['Credit', 'Month1']

但我在这里面临的挑战是列并不总是恒定的。有时它会在所有 6 个月内都有价值,但有时它不会。

例如:

                       Month1   Month2   Month3 
Credit                 5566.45  14275    6194.88  
No. of transactions       4      5        3   

预期结果

我想获取所有月份(直到 6 个月)的个人数据。如果在上述 3 个月数据示例的情况下,我想填写其余月份的值(即 Month 4Month 6'0'

谁能帮我解决这个问题?

【问题讨论】:

  • 您是否尝试使用字典而不是变量:pandas.DataFrame.to_dict ?
  • 实际上我想把所有这些单独的数据保存在 CSV 中,所以 DataFrame.to_dict 没有给我一个满意的答案
  • 遍历字典键怎么样?
  • 我该怎么做?
  • 对于 my_dict.items() 中的键、值:

标签: python python-3.x pandas dataframe


【解决方案1】:

使用to_dict。看看这是否适合你

df

                   Month1  Month2   Month3   Month4  Month5  Month6
Name                                                               
Credit             4644.5   11142  6198.33  2830.48    5886  8381.5
No.oftransactions     8.0       4     6.00    14.00       6     4.0


['No.Of Transaction{} : {}'.format(key,value) for key, value in df.to_dict(orient='index')['No.oftransactions'].items()]

['{} : {}'.format(key,value) for key, value in df.to_dict(orient='index')['Credit'].items()]

输出

['No.Of TransactionMonth1 : 8.0',
 'No.Of TransactionMonth2 : 4.0',
 'No.Of TransactionMonth3 : 6.0',
 'No.Of TransactionMonth4 : 14.0',
 'No.Of TransactionMonth5 : 6.0',
 'No.Of TransactionMonth6 : 4.0']

['Month1 : 4644.5',
 'Month2 : 11142.0',
 'Month3 : 6198.33',
 'Month4 : 2830.48',
 'Month5 : 5886.0',
 'Month6 : 8381.5']

更新

df

                   Month2   Month3   Month4  Month5  Month6
Name                                                       
Credit              11142  6198.33  2830.48    5886  8381.5
No.oftransactions       4     6.00    14.00       6     4.0

credit = df.to_dict(orient='index')['Credit']
transaction = df.to_dict(orient='index')['No.oftransactions']

['{} : {}'.format('Month{}'.format(key),credit.get('Month{}'.format(key))) for key in range(1,6) ]

输出

Out[455]:

    ['Month1 : None',
     'Month2 : 11142.0',
     'Month3 : 6198.33',
     'Month4 : 2830.48',
     'Month5 : 5886.0']


['No.Of Transaction{} : {}'.format('Month{}'.format(key),transaction.get('Month{}'.format(key))) for key in range(1,6)]

输出

['No.Of TransactionMonth1 : None',
 'No.Of TransactionMonth2 : 4.0',
 'No.Of TransactionMonth3 : 6.0',
 'No.Of TransactionMonth4 : 14.0',
 'No.Of TransactionMonth5 : 6.0']

【讨论】:

    【解决方案2】:

    您可以使用iloc 函数选择特定行,例如df.iloc[some_index]。之后,您可以手动遍历行。其他选择是在循环中使用df.iterrows()。再一次,你会得到每一行,如果你这样做for index, row in df.iterrows(),那么所有数据都在row变量中。

    更新

    for index, data in df.iterrows():
      for m in ['Month_1', .. , 'Month_6']:
        if data[m] == '':
          data[m].replace('', 0, inplace=True)
    

    【讨论】:

    • 我在问题中提到的 3 个月的案例怎么样。如果特定月份不存在,我希望其余的值用0 填充
    • 那么您可以看到行中的内容并在循环中手动填充其他值。如果row['Month_4'] 返回errornull 或任何将0 放在它的位置
    【解决方案3】:

    这确实有效并解决了我的问题

    try:
        if df.at['Credit', 'Month4']:
        SalaryMonth4 = df.at['Salary', 'Month4']
    except:
        SalaryMonth4 = 0
    

    【讨论】:

      猜你喜欢
      • 2019-01-12
      • 2018-06-08
      • 2011-05-20
      • 1970-01-01
      • 2020-05-26
      • 1970-01-01
      • 2018-09-04
      • 2021-12-06
      • 1970-01-01
      相关资源
      最近更新 更多