Python 在DataFrame中选择特定的行和列

Python 在DataFrame中选择特定的行和列,python,python-3.x,pandas,dataframe,Python,Python 3.x,Pandas,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

我的要求有点复杂

下面是一个数据帧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..
                       Month1   Month2   Month3 
Credit                 5566.45  14275    6194.88  
No. of transactions       4      5        3   
所以我想要的是,我想把所有这些值保存在一个单独的变量中

例如,最终结果应该是:

                       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..
                       Month1   Month2   Month3 
Credit                 5566.45  14275    6194.88  
No. of transactions       4      5        3   
可使用以下代码实现上述结果:

CreditMonth1= df.at['Credit', 'Month1']
但我在这里面临的挑战是,这些列并不总是恒定不变的。有时它会有价值的所有6个月,有时它不会

例如:

                       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..
                       Month1   Month2   Month3 
Credit                 5566.45  14275    6194.88  
No. of transactions       4      5        3   
预期结果

我想获得所有月份(到6个月)的个人数据。如果在上述3个月数据的示例中,我希望填写剩余的月份值(即
第4个月
第6个月
'0'


有人能帮我吗?

你可以用类似
df.iloc[some_index]
的函数选择特定的行。然后,你可以手动迭代行。其他选项是在循环中使用
df.iterrows()
。同样,你可以得到每个单独的行,如果你对索引执行
操作,则在df.iterrows()中使用行
则所有数据都在
行中
变量中

更新

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

使用
来记录
。看看这是否适合您

df

输出

['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']
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']
更新

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

输出

['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']
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']
输出

['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']
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']

这实际上起了作用,解决了我的问题

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

你是否尝试使用一个词汇表而不是变量:pandas.DataFrame.to_dict?事实上,最后我想把所有这些单独的数据保存在CSV中,所以
DataFrame.to_dict
没有给我一个令人满意的答案。如何循环使用dict键呢?我该怎么做?对于键,我的dict.items()中的值:关于我在问题中提到的3个月的情况如何。我希望用
0
填充其余的值。如果特定的月份不存在,那么您可以查看行中的内容,并手动在循环中填充其他值。如果
行['Month\u 4']
返回
error
null
或任何刚刚放置
0
的内容