2017-07-07 66 views
0

我有一個數據幀,通過打開一個csv文件並清理它而獲得。該CSV看起來是這樣的:在pandas中合併一個基於謂詞的數據框的列

"1. Do some research on al-Khorezmi (also al-Khwarizmi), the man from whose name the word ""algorithm"" is derived. In particular, you should learn what the origins of the words ""algorithm"" and ""algebra"" have in common. ",,Understand,Procedural,Understand,Factual,Understand, 
3. Write down driving directions for going from your school to your home with the precision required from an algorithm's description. ,,Apply,,Apply,Factual,Remember, 
Write down a recipe for cooking your favorite dish with the precision required by an algorithm.,Factual,Apply,,,Factual,Remember, 
"Design an algorithm to find all the common elements in two sorted lists of numbers. For example, for the lists 2, 5, 5, 5 and 2, 2, 3, 5, 5, 7, the output should be 2, 5, 5. What is the maximum number of comparisons your algorithm makes if the lengths of the two given lists are m and n, respectively?",Procedural,Create,,Apply,,, 
"a. Find gcd(31415, 14142) by applying Euclid's algorithm.",Procedural,Apply,,Apply,,, 

與此代碼加載它:

df = pd.read_csv('ADA.csv', names=['Questions', 'A1', 'A2', 'B1', 'B2', 'C1','C2']) 

這是我到目前爲止有:

          Questions   A1 \ 
5  1. Do some research on al-Khorezmi (also al-Kh...   NaN 
6  3. Write down driving directions for going fr...   NaN 
7  Write down a recipe for cooking your favorite ...  Factual 
8  Design an algorithm to find all the common ele... Procedural 
9  a. Find gcd(31415, 14142) by applying Euclid'... Procedural  

       A2   B1   B2     C1 \ 
5  Understand Procedural Understand    Factual 
6    Apply   NaN  Apply    Factual 
7    Apply   NaN   NaN    Factual 
8   Create   NaN  Apply     NaN 
9    Apply   NaN  Apply     NaN 

        C2 
5   Understand 
6    Remember 
7    Remember 
8     NaN 
9     NaN 

的列是['Questions', 'A1', 'A2', 'B1', 'B2', 'C1', 'C2']。現在

,我需要做的就是列['A1', 'B1', 'C1']一起組合成一列Label 1和列['A2', 'B2', 'C2']一起到基於這個謂詞另一列Label 2

Label 1 = A1 if A1 has a value else B1 if B1 has a value else C1 

這也將是相同的標籤2:

Label 2 = A2 if A2 has a value else B2 if B2 has a value else C2 

對於給定的輸入,我希望兩列看起來像這樣:

Label 1  Label 2 
Procedural  Understand 
Factual  Apply 
Factual  Apply 
Procedural  Create 
Procedural  Apply 

這是我的嘗試:

df['Label 1'] = df['A1'] if df['A1'] else df['B1'] if df['B1'] else df['C1'] 

但它拋出這個錯誤:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

如果我只能有一個人把我推在正確的方向,那簡直是不夠的。謝謝。

+0

IIUC'DF [ '標籤1'] = DF [ 'A1']如果DF [ 'A1'] NOTNULL()中的所有()否則DF ['B1 '] if df ['B1']。notnull()。all()else df ['C1']'。 – shivsn

+0

已用代碼,樣本csv和輸出進行編輯。 –

+0

@shivsn謝謝!它工作。請發佈該答案,我會接受。 –

回答

1

這應該工作:。

df['Label 1'] = df['A1'] if df['A1'].notnull().all() \ 
       else df['B1'] if df['B1'].notnull().all() \ 
       else df['C1'] 
相關問題