将CSV数据合并为空

时间:2016-01-28 04:25:21

标签: python csv pandas

 //  Display the square root of each number.  Remember that the user can enter negative numbers and 
//  will need to find the negative root with the "i" displayed.
printf("\nThe square root of %d is %.4f", num1, sqrt(num1));
printf("\nThe square root of %d is %.4f", num2, sqrt(num2));
printf("\nThe square root of %d is %.4f", num3, sqrt(num3));
printf("\nThe square root of %d is %.4f", num4, sqrt(num4));

返回一个空的DataFrame,我可以看到它从两个csv文件中获取数据并创建新的csv文件,但列是空的。

import pandas as pd

left = 'E:\out\outfile.csv'
right = 'E:\in\station-info.csv'
output = 'E:\out\Concatenated-Merge.csv'
left_df = pd.read_csv(left)
right_df = pd.read_csv(right, converters={'USAF': str, 'WBAN': str})
right_df["USAF_WBAN"] = right_df["USAF"] + "-" + right_df["WBAN"]
merged_df = pd.merge(left_df, right_df.ix[:, ["USAF_WBAN", "STATION NAME", "LAT", "LON"]], left_on="ID", right_on="USAF_WBAN")
merged_df.to_csv(output)
print(merged_df.head())

1 个答案:

答案 0 :(得分:0)

试试这个,你的代码稍作修改:

import pandas as pd

left = 'E:/out/outfile.csv'
right = 'E:/in/station-info.csv'
output = 'E:/out/Concatenated-Merge.csv'
left_df = pd.read_csv(left)
right_df = pd.read_csv(right, converters={'USAF': str, 'WBAN': str})
right_df["USAF_WBAN"] = right_df["USAF"] + "-" + right_df["WBAN"]
merged_df = left_df.merge(right_df.ix[:, ["USAF_WBAN", "STATION NAME", "LAT", "LON"]], left_on="ID", right_on="USAF_WBAN")
merged_df.to_csv(output)
print(merged_df.head())