Python|熊猫 Series.to_csv()
Pandas 系列是带有轴标签的一维 ndarray。标签不必是唯一的,但必须是可散列的类型。该对象支持基于整数和基于标签的索引,并提供了许多用于执行涉及索引的操作的方法。
Pandas Series.to_csv()
函数将给定的系列对象写入逗号分隔值 (csv) 文件/格式。
Syntax: Series.to_csv(*args, **kwargs)
Parameter :
path_or_buf : File path or object, if None is provided the result is returned as a string.
sep : String of length 1. Field delimiter for the output file.
na_rep : Missing data representation.
float_format : Format string for floating point numbers.
columns : Columns to write
header : If a list of strings is given it is assumed to be aliases for the column names.
index : Write row names (index).
index_label : Column label for index column(s) if desired. If None is given, and header and index are True, then the index names are used.
mode : Python write mode, default ‘w’.
encoding : A string representing the encoding to use in the output file.
compression : Compression mode among the following possible values: {‘infer’, ‘gzip’, ‘bz2’, ‘zip’, ‘xz’, None}.
quoting : Defaults to csv.QUOTE_MINIMAL.
quotechar : String of length 1. Character used to quote fields.
Returns : None or str
示例 #1:使用Series.to_csv()
函数将给定的系列对象转换为 csv 格式。
# importing pandas as pd
import pandas as pd
# Creating the Series
sr = pd.Series(['New York', 'Chicago', 'Toronto', 'Lisbon', 'Rio', 'Moscow'])
# Create the Datetime Index
didx = pd.DatetimeIndex(start ='2014-08-01 10:00', freq ='W',
periods = 6, tz = 'Europe / Berlin')
# set the index
sr.index = didx
# Print the series
print(sr)
输出 :
现在我们将使用Series.to_csv()
函数将给定的 Series 对象转换为逗号分隔的格式。
# convert to comma-separated
sr.to_csv()
输出 :
正如我们在输出中看到的, Series.to_csv()
函数已将给定的 Series 对象转换为逗号分隔的格式。示例 #2:使用Series.to_csv()
函数将给定的系列对象转换为 csv 格式。
# importing pandas as pd
import pandas as pd
# Creating the Series
sr = pd.Series([19.5, 16.8, None, 22.78, None, 20.124, None, 18.1002, None])
# Print the series
print(sr)
输出 :
现在我们将使用Series.to_csv()
函数将给定的 Series 对象转换为逗号分隔的格式。
# convert to comma-separated
sr.to_csv()
输出 :
正如我们在输出中看到的, Series.to_csv()
函数已将给定的 Series 对象转换为逗号分隔的格式。