![](/img/trans.png)
[英]How to copy a column in Pandas from one dataset to another based on condition
[英]Copy the row from one dataftrame and convert into column based on condition (pandas)
我有一个 dataframe 为:(df1)
Filename Name 1_Info1 1_Info2 1_Info3
Filename_1 Name1 First Row 01-01-1900 Information_1st Row
Filename_2 Name2 Second Row 01-01-1901 Information_2nd Row
Filename_3 Name3 Third Row 01-01-1902 Information_3rd Row
第二个dataframe为:df2(文件名)
Model Sample_ID2 Location1 Location2 Location3
MODEL_1 ID 10 40 70
MODEL_2 ID 20 50 80
MODEL_3 ID 30 60 90
MODEL_4 ID 31 61 91
MODEL_5 ID 32 62 92
MODEL_6 ID 33 63 93
df2
的Filename
在df1
的文件名列中匹配,如果匹配,则df1
的信息应复制为df2
中的列。
因此,如果文件名为“Filename_1”,output 应该如下所示。
Model Sample_ID2 Location1 Location2 Location3 Filename Name 1_Info1 1_Info2 1_Info3
MODEL_1 ID 10 40 70 Filename_1 Name1 First Row 01-01-1900 Information_1st Row
MODEL_2 ID 20 50 80 Filename_1 Name1 First Row 01-01-1900 Information_1st Row
MODEL_3 ID 30 60 90 Filename_1 Name1 First Row 01-01-1900 Information_1st Row
MODEL_4 ID 31 61 91 Filename_1 Name1 First Row 01-01-1900 Information_1st Row
MODEL_5 ID 32 62 92 Filename_1 Name1 First Row 01-01-1900 Information_1st Row
MODEL_6 ID 33 63 93 Filename_1 Name1 First Row 01-01-1900 Information_1st Row
文件名已经被提取并搜索并且行被连接但唯一的问题是在所有行中复制它(因为这仅在第一行中复制)
代码如下:
df1 = pd.read_csv("Filename_1.text.csv")
df2 = pd.read_csv("test_data.csv")
filename = os.path.basename("Filename_1.text.csv")
filename_new = filename.split('.')[0]
checked = ((df1[df1['Filename'] == filename_new]))
combined_df = pd.concat([df2, checked], axis=1)
通过使用ffill()
解决:
df1 = pd.read_csv("Filename_1.text.csv")
df2 = pd.read_csv("test_data.csv")
filename = os.path.basename("Filename_1.text.csv")
filename_new = filename.split('.')[0]
checked = ((df1[df1['Filename'] == filename_new]))
combined_df = pd.concat([df2, checked], axis=1)
cols = ['Filename','Name','1_Info1','1_Info2','1_Info3']
combined_df.loc[:,cols] = combined_df.loc[:,cols].ffill()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.