我确定我在这里遗漏了一些东西,但我无法找到它。
已将2个数据框与左连接合并。按照我的预期工作,直到我尝试在简单的字符串连接中使用生成的值。
import glob
import pandas as pd
import os
import numpy as np
import timeit
start_time = timeit.default_timer()
file_count = 1
data_df_left = {'name': ['Jack','Jill','Jenny','Ben','Lee','Kate','John','Amy','Darren','Paul'],
'classroom': ['150F','200F','250F','150F','350F','150F','350F','450F','450F','500F'],
'teacher': ['Jackson','Bird','McKay','Jackson','Yule','Jackson','Yule','Summers','Summers','Young']}
data_df_right = {'class_id': ['150F','200F','250F','300F','350F','375F','400F','425F','450F','500F'],
'classroom_sq_ft': [81.05,73.68, 89.47,75.79,86.32,78.95,73.68,80,74.74,73.68]}
left_df = pd.DataFrame(data_df_left)
right_df = pd.DataFrame(data_df_right)
left_df = pd.merge(left_df,distance_df,left_on='classroom',right_on='class_id', how='left')
left_df['classroom_detail'] = str(left_df.classroom) + ":" + str(left_df.classroom_sq_ft)
print left_df.classroom
print left_df.classroom_sq_ft
print left_df.classroom_detail
输出:
0 150F
1 200F
2 250F
3 150F
4 350F
5 150F
6 350F
7 450F
8 450F
9 500F
Name: classroom, dtype: object
0 90.53
1 93.68
2 78.95
3 90.53
4 82.11
5 90.53
6 82.11
7 94.74
8 94.74
9 88.42
Name: classroom_sq_ft, dtype: float64
0 0 150F\n1 200F\n2 250F\n3 150F\n4 ...
1 0 150F\n1 200F\n2 250F\n3 150F\n4 ...
2 0 150F\n1 200F\n2 250F\n3 150F\n4 ...
3 0 150F\n1 200F\n2 250F\n3 150F\n4 ...
4 0 150F\n1 200F\n2 250F\n3 150F\n4 ...
5 0 150F\n1 200F\n2 250F\n3 150F\n4 ...
6 0 150F\n1 200F\n2 250F\n3 150F\n4 ...
7 0 150F\n1 200F\n2 250F\n3 150F\n4 ...
8 0 150F\n1 200F\n2 250F\n3 150F\n4 ...
9 0 150F\n1 200F\n2 250F\n3 150F\n4 ...
Name: classroom_detail, dtype: object
真的期待输出类似于:
的输出0 150F:90.53
1 200F:93.68
...
提前感谢您的帮助!
答案 0 :(得分:0)
使用astype(str)
将浮点数转换为字符串:
left_df['classroom_detail'] = (left_df['classroom'] + ':'
+ left_df['classroom_sq_ft'].astype(str))
您可以使用其他字符串值系列或纯字符串添加字符串值系列。结果是逐行连接。
In [115]: left_df['classroom'] + ':' + left_df['classroom_sq_ft'].astype(str)
Out[115]:
0 150F:81.05
1 200F:73.68
2 250F:89.47
3 150F:81.05
4 350F:86.32
5 150F:81.05
6 350F:86.32
7 450F:74.74
8 450F:74.74
9 500F:73.68
dtype: object
str(left_df['classroom'])
返回系列的字符串表示形式:
In [116]: str(left_df['classroom'])
Out[116]: '0 150F\n1 200F\n2 250F\n3 150F\n4 350F\n5 150F\n6 350F\n7 450F\n8 450F\n9 500F\nName: classroom, dtype: object'
而astype(str)
将系列中的值转换为字符串:
In [119]: list(left_df['classroom_sq_ft'].astype(str))
Out[119]:
['81.05',
'73.68',
'89.47',
'81.05',
'86.32',
'81.05',
'86.32',
'74.74',
'74.74',
'73.68']