我在熊猫中有以下数据帧:
import pandas as pd
example_data = [{'ticker': 'aapl', 'loc': 'us'}, {'ticker': 'mstf', 'loc': 'us'}, {'ticker': 'baba', 'loc': 'china'}, {'ticker': 'ibm', 'loc': 'us'}, {'ticker': 'db', 'loc': 'germany'}]
df = pd.DataFrame(example_data)
print df
loc ticker
0 us aapl
1 us mstf
2 china baba
3 us ibm
4 germany db
我想创建一个新的数据帧,这样每一行都是从原始的df创建的,但是loc计数大于2的行被排除在外。也就是说,通过循环使用旧的df来创建新的df,计算前面的loc行的数量,并基于此计数包括/排除该行。你知道吗
下面的代码给出了所需的输出。你知道吗
country_counts = {}
output = []
for row in df.values:
if row[0] not in country_counts:
country_counts[row[0]] = 1
else:
country_counts[row[0]] +=1
if country_counts[row[0]] <= 2:
output.append({'loc': row[0], 'ticker': row[1]})
new_df = pd.DataFrame(output)
print new_df
loc ticker
0 us aapl
1 us mstf
2 china baba
3 germany db
输出不包括原始df中的第4行,因为其loc计数大于2(即3)。你知道吗
有没有更好的方法来执行这种类型的操作?非常感谢您的帮助。你知道吗
groupby和
.head
怎么样:另外,请注意列名,因为
loc
与.loc
方法冲突。你知道吗相关问题 更多 >
编程相关推荐