重新排列 CSV 列
rearrange CSV columns
创建 CSV 文件时,列不在我希望的正确位置。例如,“Period”列(此变量为“RD”)是文件中的第二列等
有没有办法将每一列的位置设置到我想要的位置?
我的代码:
from datetime import datetime
from elasticsearch import Elasticsearch
import csv
es = Elasticsearch(["9200"])
res = es.search(index="search", body=
{
"_source": ["VT","NCR","N","DT","RD"],
"query": {
"bool": {
"must": [{"range": {"VT": {
"gte": "now/d",
"lte": "now+1d/d"}}},
{"wildcard": {"user": "mike*"}}]}}},size=10)
csv_file = 'File_' + str(datetime.now().strftime('%Y_%m_%d - %H.%M.%S')) + '.csv'
header_names = { 'VT': 'Date', 'NCR': 'ExTime', 'N': 'Name', 'DT': 'Party', ' RD ': 'Period'}
with open(csv_file, 'w', newline='') as f:
header_present = False
for doc in res['hits']['hits']:
my_dict = doc['_source']
if not header_present:
w = csv.DictWriter(f, my_dict.keys())
w.writerow(header_names,)
header_present = True
w.writerow(my_dict)
使用pandas非常简单:
import pandas as pd
# Read csv / tab-delimited in this example
df = pd.read_csv('example.csv', sep='\t')
print df
A B C
0 4 5 9
1 4 5 9
2 4 5 9
3 4 5 9
# Reorder columns
df = df[['C', 'A', 'B']]
print df
C A B
0 9 4 5
1 9 4 5
2 9 4 5
3 9 4 5
# Write csv / tab-delimited
df.to_csv('example.csv', sep='\t')
当您处理 csv 文件时,最好使用 pandas 作为您的应用程序。
import pandas as pd
# Let your file have 4 columns named c1, c2, c3 and c4
# And assume you want to reorder it to c2, c3, c1, c4
data_frame = pd.read_csv('filename.csv', delimiter=',') # reading csv file as data frame with pandas
new_data_frame = data_frame[['c2', 'c3', 'c1', 'c4']] # reordered the dataframe and stored in new_data_frame
# If you want to save the result to new csv file
new_data_frame.to_csv('altered.csv', index=None)
在您的情况下,假设列的顺序和分隔符是“,”
import pandas as pd
csv_file_name = 'File_' + str(datetime.now().strftime('%Y_%m_%d - %H.%M.%S')) + '.csv'
data_frame = pd.read_csv(csv_file_name, delimiter=',') # change delimiter to '\t' if needed
new_data_frame = data_frame[['Party', 'Period', 'Date', 'ExTime', 'Name']]
new_data_frame.to_csv('filename.csv', index=None)
字典没有排序,如果你想强制列排序,你需要明确指定
import csv
headers = ['Party', 'Period', 'Date', 'ExTime', 'Name'] # Don't use my_dict.keys()
with open('header.csv', 'w') as f:
w = csv.DictWriter(f, fieldnames=headers)
w.writeheader()
见
$ python sample.py && cat header.csv
Party,Period,Date,ExTime,Name
并且当您调用w.writerow(my_dict)
时,字典将根据header进行排序。
row = {'Period':2, 'Date':3, 'Name':5, 'Party': 1, 'ExTime':4}
w.writerow(row)
产出
Party,Period,Date,ExTime,Name
1,2,3,4,5
import pandas as pd
df = pd.read_csv('pokemon_data.csv')`enter code here`
'Name', 'Type 1', 'Type 2', 'HP', 'Attack', 'Defense', 'Sp. Atk','Sp. Def', 'Speed', 'Generation', 'Legendary'
(我的数据框由相同顺序的相同列名组成)
df['Total']=df.iloc[:,4:10].sum(axis=1)
`'Name', 'Type 1', 'Type 2', 'HP', 'Attack', 'Defense', 'Sp. Atk','Sp. Def', 'Speed', 'Generation', 'Legendary', 'Total'`
(添加一个新列 Total,它是 hp、attack、defense、sp.atk、sp.def、speed 的总和)
cols = list(df.columns.values)
df = df[cols[0:4]+[cols[-1]]+cols[4:12]]
'Name', 'Type 1', 'Type 2','Total', 'HP', 'Attack', 'Defense', 'Sp. Atk','Sp. Def', 'Speed', 'Generation', 'Legendary'
(移动 'Total' 列)
创建 CSV 文件时,列不在我希望的正确位置。例如,“Period”列(此变量为“RD”)是文件中的第二列等
有没有办法将每一列的位置设置到我想要的位置?
我的代码:
from datetime import datetime
from elasticsearch import Elasticsearch
import csv
es = Elasticsearch(["9200"])
res = es.search(index="search", body=
{
"_source": ["VT","NCR","N","DT","RD"],
"query": {
"bool": {
"must": [{"range": {"VT": {
"gte": "now/d",
"lte": "now+1d/d"}}},
{"wildcard": {"user": "mike*"}}]}}},size=10)
csv_file = 'File_' + str(datetime.now().strftime('%Y_%m_%d - %H.%M.%S')) + '.csv'
header_names = { 'VT': 'Date', 'NCR': 'ExTime', 'N': 'Name', 'DT': 'Party', ' RD ': 'Period'}
with open(csv_file, 'w', newline='') as f:
header_present = False
for doc in res['hits']['hits']:
my_dict = doc['_source']
if not header_present:
w = csv.DictWriter(f, my_dict.keys())
w.writerow(header_names,)
header_present = True
w.writerow(my_dict)
使用pandas非常简单:
import pandas as pd
# Read csv / tab-delimited in this example
df = pd.read_csv('example.csv', sep='\t')
print df
A B C
0 4 5 9
1 4 5 9
2 4 5 9
3 4 5 9
# Reorder columns
df = df[['C', 'A', 'B']]
print df
C A B
0 9 4 5
1 9 4 5
2 9 4 5
3 9 4 5
# Write csv / tab-delimited
df.to_csv('example.csv', sep='\t')
当您处理 csv 文件时,最好使用 pandas 作为您的应用程序。
import pandas as pd
# Let your file have 4 columns named c1, c2, c3 and c4
# And assume you want to reorder it to c2, c3, c1, c4
data_frame = pd.read_csv('filename.csv', delimiter=',') # reading csv file as data frame with pandas
new_data_frame = data_frame[['c2', 'c3', 'c1', 'c4']] # reordered the dataframe and stored in new_data_frame
# If you want to save the result to new csv file
new_data_frame.to_csv('altered.csv', index=None)
在您的情况下,假设列的顺序和分隔符是“,”
import pandas as pd
csv_file_name = 'File_' + str(datetime.now().strftime('%Y_%m_%d - %H.%M.%S')) + '.csv'
data_frame = pd.read_csv(csv_file_name, delimiter=',') # change delimiter to '\t' if needed
new_data_frame = data_frame[['Party', 'Period', 'Date', 'ExTime', 'Name']]
new_data_frame.to_csv('filename.csv', index=None)
字典没有排序,如果你想强制列排序,你需要明确指定
import csv
headers = ['Party', 'Period', 'Date', 'ExTime', 'Name'] # Don't use my_dict.keys()
with open('header.csv', 'w') as f:
w = csv.DictWriter(f, fieldnames=headers)
w.writeheader()
见
$ python sample.py && cat header.csv
Party,Period,Date,ExTime,Name
并且当您调用w.writerow(my_dict)
时,字典将根据header进行排序。
row = {'Period':2, 'Date':3, 'Name':5, 'Party': 1, 'ExTime':4}
w.writerow(row)
产出
Party,Period,Date,ExTime,Name
1,2,3,4,5
import pandas as pd
df = pd.read_csv('pokemon_data.csv')`enter code here`
'Name', 'Type 1', 'Type 2', 'HP', 'Attack', 'Defense', 'Sp. Atk','Sp. Def', 'Speed', 'Generation', 'Legendary'
(我的数据框由相同顺序的相同列名组成)
df['Total']=df.iloc[:,4:10].sum(axis=1)
`'Name', 'Type 1', 'Type 2', 'HP', 'Attack', 'Defense', 'Sp. Atk','Sp. Def', 'Speed', 'Generation', 'Legendary', 'Total'`
(添加一个新列 Total,它是 hp、attack、defense、sp.atk、sp.def、speed 的总和)
cols = list(df.columns.values)
df = df[cols[0:4]+[cols[-1]]+cols[4:12]]
'Name', 'Type 1', 'Type 2','Total', 'HP', 'Attack', 'Defense', 'Sp. Atk','Sp. Def', 'Speed', 'Generation', 'Legendary'
(移动 'Total' 列)