我正在创建一个dataframe,方法是根据enduser在一个选择列表上所做的选择,对一个非常大的dataframe (大约400列)进行配对。选择列表之一是enduser想要的分母类型。下面是一个示例表,其中包含了最后计算之前的所有信息。
county _tcount _tvote _f_npb_18_count _f_npb_18_vote
countycode
35 San Benito 28194 22335 2677 1741
36 San Bernardino 912653 661838 108724 61832
countycode _f_npb_30_count _f_npb_30_vote
35 384 288
36 76749 53013但是,我在创建代码时遇到了麻烦,该代码将自动将从第5列开始的每一列(不包括索引)除以前面的列(跳过其他列)。我见过示例(Divide multiple columns by another column in pandas),但它们都使用固定的列名,这在这方面是无法实现的。我可以通过固定列(基于位置)变量列,但不能通过其他基于位置的变量列来变量列。我尝试根据列位置修改上面链接中的代码:
calculated_frame = [county_select_frame[county_select_frame.columns[5: : 2]].div(county_select_frame[4: :2], axis=0)]产出:
[ county _tcount _tvote _f_npb_18_count _f_npb_18_vote \
countycode
35 NaN NaN NaN NaN NaN
36 NaN NaN NaN NaN NaN]RuntimeWarning:在更大的(abs_vals >0).any()中遇到的无效值
当股息是固定的[5: :2]时,使用field.If是有效的--我不能让它起作用,这不是什么大事(但是如果有我想要的所有选项,那就太好了)。
发布于 2017-04-24 05:49:50
我认为您可以用由numpy array创建的values除以,因为这样就不能对齐列名。最后,通过构造函数创建新的DataFrame:
arr = county_select_frame.values
df1 = pd.DataFrame(arr[:,5::2] / arr[:,4::2], columns = county_select_frame.columns[5::2])示例:
np.random.seed(10)
county_select_frame = pd.DataFrame(np.random.randint(10, size=(10,10)),
columns=list('abcdefghij'))
print (county_select_frame)
a b c d e f g h i j
0 9 4 0 1 9 0 1 8 9 0
1 8 6 4 3 0 4 6 8 1 8
2 4 1 3 6 5 3 9 6 9 1
3 9 4 2 6 7 8 8 9 2 0
4 6 7 8 1 7 1 4 0 8 5
5 4 7 8 8 2 6 2 8 8 6
6 6 5 6 0 0 6 9 1 8 9
7 1 2 8 9 9 5 0 2 7 3
8 0 4 2 0 3 3 1 2 5 9
9 0 1 0 1 9 0 9 2 1 1
arr = county_select_frame.values
df1 = pd.DataFrame(arr[:,5::2] / arr[:,4::2], columns = county_select_frame.columns[5::2])
print (df1)
f h j
0 0.000000 8.000000 0.000000
1 inf 1.333333 8.000000
2 0.600000 0.666667 0.111111
3 1.142857 1.125000 0.000000
4 0.142857 0.000000 0.625000
5 3.000000 4.000000 0.750000
6 inf 0.111111 1.125000
7 0.555556 inf 0.428571
8 1.000000 2.000000 1.800000
9 0.000000 0.222222 1.000000发布于 2017-04-24 07:58:33
我倾向于通过设置索引和使用filter分别划分计数和选票数据来组织它。然后使用join
d1 = df.set_index('county', append=True)
counts = d1.filter(regex='.*_\d+_count$').rename(columns=lambda x: x.replace('_count', ''))
votes = d1.filter(regex='.*_\d+_vote$').rename(columns=lambda x: x.replace('_vote', ''))
d1[['_tcount', '_tvote']].join(votes / counts)
_tcount _tvote _f_npb_18 _f_npb_30
countycode county
35 San Benito 28194 22335 0.650355 0.750000
36 San Bernardino 912653 661838 0.568706 0.690732发布于 2017-04-24 05:55:55
不如像这样
cols = my_df.columns
for i in range(2, 6):
print(u'Creating new col %s', cols[i])
my_df['new_{0}'.format(cols[i]) = my_df[cols[i]] / my_df[cols[i-1] https://stackoverflow.com/questions/43580566
复制相似问题