以下是一個示例腳本。您可以將loc
方法與不存在的密鑰一起使用至enlarge the DataFrame
並設置新行的值。
最簡單的解決方案似乎是創建一個模板DataFrame
作爲分隔符與所需的值設置。然後將其插入到數據框的列表中以在合適的位置連接。
最後,我刪除了chdir
,因爲glob
可以在任何路徑中搜索。
import glob
import pandas
def concatenate(input_dir, output_file_name):
file_list=glob.glob(input_dir + "/*.csv")
column_names=["Creation Date"
, "Author"
, "Tweet"
, "Language"
, "Location"
, "Country"
, "Continent"]
# Create a separator template
separator = pandas.DataFrame(columns=column_names)
separator.loc[0] = [""]*7
dataframes = []
for file_name in file_list:
print(file_name)
if len(dataframes):
# The list is not empty, so we need to add a separator
dataframes.append(separator)
dataframes.append(pandas.read_csv(file_name))
concatenated = pandas.concat(dataframes, axis=0)
concatenated.to_csv(output_file_name, index=None)
print(concatenated)
concatenate("input", ".out.csv")
一種替代,甚至更短,方法是建立級聯DataFrame
迭代,使用append
方法。
def concatenate(input_dir, output_file_name):
file_list=glob.glob(input_dir + "/*.csv")
column_names=["Creation Date"
, "Author"
, "Tweet"
, "Language"
, "Location"
, "Country"
, "Continent"]
concatenated = pandas.DataFrame(columns=column_names)
for file_name in file_list:
print(file_name)
if len(concatenated):
# The list is not empty, so we need to add a separator
concatenated.loc[len(concatenated)] = [""]*7
concatenated = concatenated.append(pandas.read_csv(file_name))
concatenated.to_csv(output_file_name, index=None)
print(concatenated)
我測試了3個輸入CSV文件的腳本:
輸入/ 1.csv
Creation Date,Author,Tweet,Language,Location,Country,Continent
2015-12-17,foo,Hello,EN,London,UK,Europe
2015-12-18,bar,Bye,EN,Manchester,UK,Europe
2015-12-28,baz,Hallo,DE,Frankfurt,Germany,Europe
輸入/ 2.csv
Creation Date,Author,Tweet,Language,Location,Country,Continent
2016-01-09,bar,Tweeeeet,EN,New York,USA,America
2016-01-09,cat,Miau,FI,Helsinki,Finland,Europe
輸入/ 3.csv
Creation Date,Author,Tweet,Language,Location,Country,Continent
2018-12-12,who,Hello,EN,Delhi,India,Asia
當我跑它,下面的輸出被寫入到控制檯:
控制檯輸出(使用的concat)
input\1.csv
input\2.csv
input\3.csv
Creation Date Author Tweet Language Location Country Continent
0 2015-12-17 foo Hello EN London UK Europe
1 2015-12-18 bar Bye EN Manchester UK Europe
2 2015-12-28 baz Hallo DE Frankfurt Germany Europe
0
0 2016-01-09 bar Tweeeeet EN New York USA America
1 2016-01-09 cat Miau FI Helsinki Finland Europe
0
0 2018-12-12 who Hello EN Delhi India Asia
較短變體的控制檯輸出稍有不同(請注意th中的索引第一列),但這對生成的CSV文件沒有影響。
控制檯輸出(使用追加)
input\1.csv
input\2.csv
input\3.csv
Creation Date Author Tweet Language Location Country Continent
0 2015-12-17 foo Hello EN London UK Europe
1 2015-12-18 bar Bye EN Manchester UK Europe
2 2015-12-28 baz Hallo DE Frankfurt Germany Europe
3
0 2016-01-09 bar Tweeeeet EN New York USA America
1 2016-01-09 cat Miau FI Helsinki Finland Europe
6
0 2018-12-12 who Hello EN Delhi India Asia
最後,這就是輸出CSV文件生成它看起來像:
out.csv
Creation Date,Author,Tweet,Language,Location,Country,Continent
2015-12-17,foo,Hello,EN,London,UK,Europe
2015-12-18,bar,Bye,EN,Manchester,UK,Europe
2015-12-28,baz,Hallo,DE,Frankfurt,Germany,Europe
,,,,,,
2016-01-09,bar,Tweeeeet,EN,New York,USA,America
2016-01-09,cat,Miau,FI,Helsinki,Finland,Europe
,,,,,,
2018-12-12,who,Hello,EN,Delhi,India,Asia
請確定您的代碼格式爲co正確地說,目前縮進被打破了,這在Python中是一個阻擋。 –
我的代碼工作正常。這只是我希望添加的空白行。僅僅因爲你不認爲縮進是正確的,意味着我得到了一個downvote? – bonbon
不是因爲我認爲這是錯誤的,而是因爲[它實際上是錯誤的](https://ideone.com/yuQgvL)。第4行的功能沒有主體。從第5行開始,您失去了縮進級別。也許只有當你寫出你的問題時,但這並不能改變它不正確的事實。因此,這個問題缺乏質量,對我來說它說你沒有花足夠的精力去寫它。當這個變化時,我會很樂意重新評估我的投票。 –