[英]new column in Pandas python based on condition
我對熊貓很陌生,因此,我需要你們專家的幫助!
我對從多行連接數據感到非常困惑。
#copy selected row and column. Set specific column into a appropriate data type
filep2 = pd.read_csv(r'/Users/syafiq/Downloads/RoutingPractice01/my_raw.csv')
#set column date/time and idd datatype
filep2['Received Date/Time']= filep2['Received Date/Time'].astype('datetime64[ns]')
filep2['IDD']=filep2['IDD'].astype(str)
#select the specific column
df = pd.DataFrame(filep2, columns=['Account User Name','Sender','Body','IDD','Received Date/Time'])
df2= df.loc[df['IDD'].notnull(),['Account User Name','Sender','Body','IDD','Received Date/Time']]
df = pd.DataFrame(df2)
#create new column as Concat
concat =''
df['Concat']=concat
print(df2)
我的 csv 文件“my_raw.csv”包含數千行 15 列。
而正文列內容一條消息。 IDD 列包含該特定正文消息的分配鍵。
例如在我的csv文件中,如下;
Sender Body UDH Date/Time
ABC Hello CD30010101 01/01/20 1:57
ABC John CD30010102 01/01/20 1:58
XYZ Please Wait for your turn 3300020201 01/01/20 17:57
XYZ While waiting for our staff 3300020202 01/01/20 17:58
XYZ To Complete his task 3300020203 01/01/20 17:59
ABC Your Parcel is Here 1100DCB001 03/01/20 11:57
正如您在上面看到的,我想將正文連接成一行並復制到一個新列中(我已經將其命名為 Concat)。 在選擇收到消息的最后日期時。
例如:帶有UDH CD30010101和CD30010102的ABC,正文消息需要組合在一起並復制到一個新列中。 同時將收到的最后日期/時間(即 01/01/20 1:58)也復制到新列中。
期望輸出:
Concat Date/Time
(ROW 1) Hello John 01/01/20 1:58
(ROW 2) Please wait for your turn while waiting
for our staff to complete his task 01/01/20 17:59
我已經試了好幾天了,仍然沒有骰子,繼續撞磚牆。
需要您的指導和專業知識!!
非常感謝和感謝!
歡迎使用堆棧溢出。
根據您的文件實際列名和所需列名進行編輯:
我只是意識到您想按Sender
以及以相同字符開頭的UHG
對它們進行分組(有多少?您沒有指定,所以我在下面的示例中只使用 5 個字符)。
以下應該達到您的要求:
def concat_series_to_string(series):
return ' '.join(series)
df['Received Date/Time'] = pd.to_datetime(df['Received Date/Time'])
# Update according to your actual session identification method.
df['UDH_session'] = df['UDH'].str[:5]
df_concat = df\
.groupby(['Sender','UDH_session'])\
.agg({'Body':[concat_series_to_string],'Received Date/Time':['last']})\
df_concat.columns = ['Concat','Received Date/Time (last)']
df_concat = df_concat.sort_values('Received Date/Time (last)')
您可以嘗試以下操作:
df = pd.read_csv(<csv file name>)
df_concat = pd.DataFrame(columns=['Concat', 'Date/Time'])
df_concat['Concat'] = df.groupby('Sender')['Body'].apply(' '.join)
df_concat['Date/Time'] = df.groupby('Sender')['Date/Time'].last()
我假設您只想(幾乎)連接同一發件人的連續行。 事實上,所需的操作可能更像是一個' '.join
以確保連續行之間的空間。
您可以使用(df['Sender'] != df['Sender'].shift()).cumsum()
識別具有相同發件人的連續行上的組。
最后,你可以這樣做:
resul = df.groupby((df['Sender'] != df['Sender'].shift()).cumsum()
).agg({'Sender': 'first', 'Body': ' '.join, 'Date/Time': max}
).rename_axis(None)
它給:
Sender Body Date/Time
1 ABC Hello John 01/01/20 1:58
2 XYZ Please Wait for your turn While waiting for ou... 01/01/20 17:59
3 ABC Your Parcel is Here 03/01/20 11:57
我想通了,通過在 UDH 列中使用 .str[:-1] ,同時選擇接收日期/時間的最后一個日期。
groups = df.groupby([df['UDH'].str[:-1], 'Original Sender ID'])
df = groups.agg({'Body':''.join, 'Received Date/Time':max}).reset_index()
df = df.sort_values('Received Date/Time')
pd.options.display.width = 200
print(df.sort_values('Received Date/Time'))
希望它可以幫助那些試圖實施同樣事情的其他人
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.