![](/img/trans.png)
[英]how to create columns from another dataframe rows and populate them based on values from both dataframes
[英]create a new dataframes with rows from another dataframe with specific columns values
我想從現有的 dataframe 創建一個新的 pandas dataframe,其中只有特定列值大於 x 的行。 例如:
df = A B C
0 1 0 2
1 2 1 3
2 3 3 0
3 4 2 5
我想只使用列 B 和 C 的值大於 0 的行創建 new_df
new_df = A B C
0 2 1 3
1 4 2 5
我使用了命令:
new_df = df.loc[df['B'] > 0 & df['C'] > 0]
但它不起作用
你幾乎擁有它。 使用括號括起來條件:
new_df = df.loc[(df['B'] > 0) & (df['C'] > 0)]
二元運算符&
和|
優先於平等/不平等。 所以你所做的相當於:
new_df = df.loc[(df['B'] > (0 & df['C'])) > 0]
你可以使用這個:
df[(df[['B', 'C']] > 0).all(axis=1)]
Output:
A B C
1 2 1 3
3 4 2 5
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.