![](/img/trans.png)
[英]how to copy values from one column into another column based on conditions of two different columns in pandas?
[英]How to find row index of one column based on values of a different column where the values in the two distinct columns are equal in pandas?
我有一些熊貓輸出:
seq X1 X2
0 0.59 NaN
1 -1.28 NaN
2 -1.26 NaN
3 -0.79 NaN
4 1.03 NaN
5 -1.43 NaN
6 0.03 1.03
7 0.92 1.03
8 -2.21 1.03
如何獲得第三列X3,該列在X2中為1.03,並在X1列中找到與相同編號相關聯的seq號? 在我的示例中,從row7(行索引6)開始,X3應該返回4,因為當X1為1.03時seq = 4。
我渴望:
seq X1 X2 X3
0 0.59 NaN NaN
1 -1.28 NaN NaN
2 -1.26 NaN NaN
3 -0.79 NaN NaN
4 1.03 NaN NaN
5 -1.43 NaN NaN
6 0.03 1.03 4
7 0.92 1.03 4
8 -2.21 1.03 4
有史以來第一個堆棧問題。 請原諒我的愚蠢!
您能解釋一下為什么要讓數字4
出現在X3
所有行中嗎?
您可以通過鍵入以下命令獲得seq值( 4
),其中X1 == 1.03
:
df.loc[df['X1']==1.03, 'seq'].values[0]
但這只給您4。請注意,我采用了第一個seq值(通過鍵入[0]
),因為如果您在X1 == 1.03
的多個位置上,將返回數字列表(作為數據幀) ),而您尚未說明如何處理多個seq匹配。
以下代碼將運行並返回您請求的數據幀,但是我建議您花一些時間考慮是否需要X2
和X3
完全成為數據幀的一部分...
# Import what you need
import pandas as pd
import numpy as np
# Define the data
x1 = np.array([0.59, -1.28, -1.27, -0.79, \
1.03, -1.43, 0.03, 0.92, -2.21])
x2 = np.array([np.nan, np.nan, np.nan, np.nan, \
np.nan, np.nan, 1.03, 1.03, 1.03])
# Create a pandas dataframe
df = pd.DataFrame( { 'seq' : range(9),
'X1' : x1,
'X2' : x2 } )
# Figure out where the first instance of X1==1.03
# occurs and grab that seq value
s_first = df.loc[df['X1']==1.03,'seq'].values[0]
# Fill in X3 according to the values in X2
df.loc[df['X2'].isnull(), 'X3'] = np.nan
df.loc[df['X2'].notnull(), 'X3'] = s_first
# Show the 9 rows in the data frame
df.head(9)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.