[英]how to append a dataframe to existing csv file and remove the duplicates data
[英]How to append header to the data in csv file created by python dataframe
我在數據框中有可用數據
EMP_ID,EMP_NAME,AGE
1,X,71
2,Y,70
3,Z,78
如何將以上數據轉換為以下格式
EMP_ID:1|EMP_NAME:X|AGE:71
EMP_ID:2|EMP_NAME:Y|AGE:70
EMP_ID:3|EMP_NAME:Z|AGE:78
#
反之亦然:
我們可以轉換以下數據
EMP_ID,EMP_NAME,AGE
1,X,71
2,Y,70
3,Z,78
再次回到
EMP_ID,EMP_NAME,AGE 1,X,71 2,Y,70 3,Z,78
df.columns = df.columns +'|'+ df.iloc [0 ,:]
這可能不是最有效的選擇,但是您可以遍歷dataframes列,然后對每個列應用基本功能。
注意:您的所有列現在必須是字符串。
for col in df.columns:
df[col] = df[col].apply(lambda x: col +':' + str(x))
為了扭轉它,請使用類似的邏輯使用字符串切片:
for col in df.columns:
df[col] = df[col].apply(lambda x: x[len(col)+1:])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.