附加两个 csv 文件时如何修复 pandas concat
How to fix pandas concat when appending of two csv files
我正在尝试将两个具有相同结构的 csv
文件合并为一个合并的 csv
文件,但是当我这样做时,列以不同的顺序重新排列,我丢失了其中一个的数据我的 csv
个文件。
我已经检查了这两个文件以确保它们的列彼此一致但是当我执行我的代码时我的 csv
文件之一(变量 b)似乎在 "Social Care DTOC beds" 柱子。执行代码后我也收到此消息:
"FutureWarning: Sorting because non-concatenation axis is not aligned. A future version of pandas will change to not sort by default. To accept the future behavior, pass 'sort=True'
. To retain the current behavior and silence the warning, pass sort=False
"
a=pd.read_csv('https://www.england.nhs.uk/statistics/wp-content/uploads/sites/2/2019/04/MSitDT-FEBRUARY-2019-full-extract-for-publication-td5dtd.csv')
b = pd.read_csv('https://www.england.nhs.uk/statistics/wp-content/uploads/sites/2/2019/03/MSitDT-JANUARY-2019-full-extract-for-publication-5tsrt.csv')
out_put=pd.concat([a,b])
out_put.to_csv( "result.csv", encoding='utf-8-sig')
我希望得到一个 csv 文件,其中的组合行按相同的列顺序排列,并且没有任何数据丢失。
列 headers 之间存在大小写 mis-matching... 例如。 "Social Care DTOC Beds"
vs "Social Care DTOC beds"
- 注意 'beds'.
中的小写 'b'
简单的解决方法是标准化这些列 headers,如果您愿意,可以使用 str.title
(or str.lower
/ str.upper
):
a=pd.read_csv('https://www.england.nhs.uk/statistics/wp-content/uploads/sites/2/2019/04/MSitDT-FEBRUARY-2019-full-extract-for-publication-td5dtd.csv')
b = pd.read_csv('https://www.england.nhs.uk/statistics/wp-content/uploads/sites/2/2019/03/MSitDT-JANUARY-2019-full-extract-for-publication-5tsrt.csv')
a.columns = a.columns.str.title()
b.columns = b.columns.str.title()
out_put = pd.concat([a, b])[a.columns]
我正在尝试将两个具有相同结构的 csv
文件合并为一个合并的 csv
文件,但是当我这样做时,列以不同的顺序重新排列,我丢失了其中一个的数据我的 csv
个文件。
我已经检查了这两个文件以确保它们的列彼此一致但是当我执行我的代码时我的 csv
文件之一(变量 b)似乎在 "Social Care DTOC beds" 柱子。执行代码后我也收到此消息:
"FutureWarning: Sorting because non-concatenation axis is not aligned. A future version of pandas will change to not sort by default. To accept the future behavior, pass 'sort=True'
. To retain the current behavior and silence the warning, pass sort=False
"
a=pd.read_csv('https://www.england.nhs.uk/statistics/wp-content/uploads/sites/2/2019/04/MSitDT-FEBRUARY-2019-full-extract-for-publication-td5dtd.csv')
b = pd.read_csv('https://www.england.nhs.uk/statistics/wp-content/uploads/sites/2/2019/03/MSitDT-JANUARY-2019-full-extract-for-publication-5tsrt.csv')
out_put=pd.concat([a,b])
out_put.to_csv( "result.csv", encoding='utf-8-sig')
我希望得到一个 csv 文件,其中的组合行按相同的列顺序排列,并且没有任何数据丢失。
列 headers 之间存在大小写 mis-matching... 例如。 "Social Care DTOC Beds"
vs "Social Care DTOC beds"
- 注意 'beds'.
简单的解决方法是标准化这些列 headers,如果您愿意,可以使用 str.title
(or str.lower
/ str.upper
):
a=pd.read_csv('https://www.england.nhs.uk/statistics/wp-content/uploads/sites/2/2019/04/MSitDT-FEBRUARY-2019-full-extract-for-publication-td5dtd.csv')
b = pd.read_csv('https://www.england.nhs.uk/statistics/wp-content/uploads/sites/2/2019/03/MSitDT-JANUARY-2019-full-extract-for-publication-5tsrt.csv')
a.columns = a.columns.str.title()
b.columns = b.columns.str.title()
out_put = pd.concat([a, b])[a.columns]