简体   繁体   English

如何为数据框中的每一行分配一个值到不同的列?

[英]How can I assign a value to a different column for each row in a dataframe?

I have a dataframe dat that looks like this: 我有一个看起来像这样的数据帧dat

        p1    p2    type  replace
1       0     1     1     1
2       1     0     1     1
3       0     0     2     1
...

I want do something like dat['p + str(type)'] = replace to get: 我想做类似dat['p + str(type)'] = replace操作:

        p1    p2    type  replace 
1       1     1     1     1
2       1     0     1     1
3       0     1     2     1
...

How can I do this? 我怎样才能做到这一点? Of course I can't assign in a loop using something like iterrows... 当然我不能使用类似iterrows的循环分配...

Maybe there is some one liner to do this, but if performance is not really an issue, you can easily do this with a simple for loop: 也许有一些衬板可以做到这一点,但是如果性能并不是真正的问题,则可以使用简单的for循环轻松地做到这一点:

In [134]: df
Out[134]: 
   p1  p2  type  replace
0   0   1     1        1
1   1   0     1        1
2   0   0     2        1

In [135]: for i in df.index:
     ...:     df.loc[i, 'p'+str(df.loc[i, 'type'])] = df.loc[i, 'replace']

In [136]: df
Out[136]: 
   p1  p2  type  replace
0   1   1     1        1
1   1   0     1        1
2   0   1     2        1

If you have much more rows than columns, this will be much faster and is actually easier (and if necessary you can loop over 1, 2, ..): 如果行多于列,这将更快并且实际上更容易(并且如有必要,可以循环1、2,..):

df["p1"][df["type"]==1] = df["replace"][df["type"]==1]
df["p2"][df["type"]==2] = df["replace"][df["type"]==2]
In [47]: df['p1'].where(~(df['type'] == 1), df['replace'], inplace=True)

In [48]: df['p2'].where(~(df['type'] == 2), df['replace'], inplace=True)

In [49]: df
Out[49]: 
   p1  p2  type  replace
1   1   1     1        1
2   1   0     1        1
3   0   1     2        1

Just for completeness, I ended up doing the following, which may or may not be the same as what Dan Allan suggested: 为了完整起见,我最终做了以下操作,该操作可能与Dan Allan所建议的相同或不同:

for i in range(2):
    df.loc[df['type'] == i + 1, 'p' + str(i + 1)] = df.loc[df['type'] == i + 1, 'replace']

I have a much larger problem than the example I gave (with something like 30 types and thousands of rows in the dataframe), and this solution seems very fast. 我有一个比我给出的示例更大的问题(在数据框中有30种类型和数千行),这种解决方案似乎非常快。 Thanks to all for your help in thinking about this problem! 感谢大家在思考这个问题上的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 迭代 dataframe 并为每一行分配值 - 我得到相同的值,但我想要不同的值 - Iterate dataframe and assign value to each row- I get the same value while I want different ones 如何用 Pyspark Dataframe 分配一行? - how can I assign a row with Pyspark Dataframe? Python:如何在每一列中为每行中的给定总数分配值 - Python: How to assign value in each column with a given total in each row 如何在列中的每个不同值上拆分 DataFrame? - How to split a DataFrame on each different value in a column? 如何在 DataFrame 的同一列上复制前一行的值? - How can I copy the value of a previous row, on the same column in a DataFrame? 如果每列每行有多个值,如何在熊猫数据框中的两列之间创建字典? - How can I create a dictionary between two columns within a pandas dataframe if each column has more than one value per row? 如何测试每一行的列值? - How can I test the value of a column for each row? 如何替换每一行的日期时间列中的“年”值? - How can I replace the 'year' value in a datetime column for each row? 如何使数据框中的每一行每一列都有一个值? - How to make each row in dataframe have one value for each column? 如何将每行包含一个字典的 DataFrame 列解析为独立的 DataFrame 列? - How can I parse a DataFrame column where each row contains a Dict, into independent DataFrame columns?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM