在DataFrame中组合多个列 [英] Combining multiple columns in a DataFrame
本文介绍了在DataFrame中组合多个列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
import numpy as np
import pandas as pd
df = pd.DataFrame(np.random.binomial(1,0.2,(100,40)))
new_df [0-3] = df [ 0] + df [1] + df [2] + df [3]
/ pre>
new_df [4-7] = df [4] + df [5] + df [6] + df [7]
...
new_df [36-39] = df [36] + df [37] + df [38] + df [39]
我可以在单个语句中执行此操作(还是以比单独求和更好的方式)?新的DataFrame中的列名不重要。
解决方案这是另一种方法:
new_df = df.transpose()
new_df ['Group'] = new_df.index / 4
new_df = new_df.groupby 'group')。sum()。transpose()
请注意,这里的分割操作是整数除法,而不是浮点除法。
I have a DataFrame with 40 columns (columns 0 through 39) and I want to group them four at a time:
import numpy as np import pandas as pd df = pd.DataFrame(np.random.binomial(1, 0.2, (100, 40)))
new_df["0-3"] = df[0] + df[1] + df[2] + df[3] new_df["4-7"] = df[4] + df[5] + df[6] + df[7] ... new_df["36-39"] = df[36] + df[37] + df[38] + df[39]
Can I do this in a single statement (or in a better way than summing them separately)? The column names in the new DataFrame are not important.
解决方案Here's another way to do it:
new_df = df.transpose() new_df['Group'] = new_df.index / 4 new_df = new_df.groupby('Group').sum().transpose()
Note that the divide-by operation here is integer division, not floating-point division.
这篇关于在DataFrame中组合多个列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文