Pandas 数据框保存到 csv 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46143726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:25:46  来源:igfitidea点击:

Pandas Data Frame saving into csv file

pythonpandascsv

提问by Elham

I wonder how to save a new pandas Series into a csv file in a different column. Suppose I have two csv files which both contains a column as a 'A'. I have done some mathematical function on them and then create a new variable as a 'B'.

我想知道如何将新的Pandas系列保存到不同列中的 csv 文件中。假设我有两个 csv 文件,它们都包含一列作为“A”。我对它们做了一些数学函数,然后创建了一个新变量作为“B”。

For example:

例如:

data = pd.read_csv('filepath')

data['B'] = data['A']*10

# and add the value of data.B into a list as a B_list.append(data.B) 

This will continue until all of the rows of the first and second csv file has been reading.

这将一直持续到第一个和第二个 csv 文件的所有行都已被读取。

I would like to save a column B in a new spread sheet from both csv files. For example I need this result:

我想在两个 csv 文件的新电子表格中保存 B 列。例如我需要这个结果:

colum1(from csv1)        colum2(from csv2)     
     data.B.value             data.b.value

By using this code:

通过使用此代码:

pd.DataFrame(np.array(B_list)).T.to_csv('file.csv', index=False, header=None)

I won't get my preferred result.

我不会得到我想要的结果。

回答by Parfait

Since each column in a pandas DataFrameis a pandas Series. Your B_listis actually a list of pandas Serieswhich you can cast to DataFrame()constructor, then transpose (or as @jezrael shows a horizontal merge with pd.concat(..., axis=1))

因为 pandasDataFrame中的每一列都是一个 pandas Series。您的B_list实际上是一个Pandas列表Series,您可以将其DataFrame()转换为构造函数,然后转置(或如@jezrael 显示与 水平合并pd.concat(..., axis=1)

finaldf = pd.DataFrame(B_list).T
finaldf.to_csv('output.csv', index=False, header=None)

And should csv have different rows, unequal series are filled with NANs at corresponding rows.

并且如果 csv 有不同的行,不等的系列在相应的行用 NAN 填充。

回答by jezrael

I think you need concatcolumn from data1with column from data2first:

我认为您首先需要concat列来自data1data2

df = pd.concat(B_list, axis=1)
df.to_csv('file.csv', index=False, header=None)