简体   繁体   English

熊猫结合两列

[英]Pandas combine two columns

I have following database: 我有以下数据库:

df = pandas.DataFrame({'Buy':[10,np.nan,2,np.nan,np.nan,4],'Sell':[np.nan,7,np.nan,9,np.nan,np.nan]})

Out[37]: 
    Buy  Sell
0  10.0   NaN
1   NaN   7.0
2   2.0   NaN
3   NaN   9.0
4   NaN   NaN
5   4.0   NaN

I want o create two more columns called Quant and B/S 我想再创建两个名为QuantB/S

for Quant it is working fine as follows: 对于Quant它可以按以下方式正常工作:

df['Quant'] = df['Buy'].fillna(df['Sell']) # Fetch available value from both column and if both values are Nan then output is Nan . df ['Quant'] = df ['Buy']。fillna(df ['Sell'])#从两个列中获取可用值,如果两个值均为Nan则输出为Nan

Output is: 输出为:

df
Out[39]: 
    Buy  Sell  Quant
0  10.0   NaN   10.0
1   NaN   7.0    7.0
2   2.0   NaN    2.0
3   NaN   9.0    9.0
4   NaN   NaN    NaN
5   4.0   NaN    4.0

But I want to create B/S on the basis of "from which column they have taken value while creating Quant " 但是我想基于“他们在创建Quant从哪个列中获取了价值”来创建B/S

You can perform an equality test and feed into numpy.where : 您可以执行相等性测试并将其输入numpy.where

df['B/S'] = np.where(df['Quant'] == df['Buy'], 'B', 'S')

For the case where both values are null, you can use an additional step: 对于两个值都为空的情况,可以使用附加步骤:

df.loc[df[['Buy', 'Sell']].isnull().all(1), 'B/S'] = np.nan

Example

from io import StringIO
import pandas as pd

mystr = StringIO("""Buy    Sell
10      nan
nan      8
4       nan
nan      5
nan      7
3       nan
2       nan
nan     nan""")

df = pd.read_csv(mystr, delim_whitespace=True)

df['Quant'] = df['Buy'].fillna(df['Sell'])
df['B/S'] = np.where(df['Quant'] == df['Buy'], 'B', 'S')
df.loc[df[['Buy', 'Sell']].isnull().all(1), 'B/S'] = np.nan

Result 结果

print(df)

    Buy  Sell  Quant  B/S
0  10.0   NaN   10.0    B
1   NaN   8.0    8.0    S
2   4.0   NaN    4.0    B
3   NaN   5.0    5.0    S
4   NaN   7.0    7.0    S
5   3.0   NaN    3.0    B
6   2.0   NaN    2.0    B
7   NaN   NaN    NaN  NaN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM