2017-08-29 55 views
2

目前,我从文件中读取,它是生成此文件(output.txt):如果列值来自不同的文件,如何将列插入数据框?

Atom nVa avgppm stddev delta 
1.H1' 2 5.73649 0.00104651803616 1.0952e-06 
1.H2' 1 4.85438 
1.H8 1 8.05367 
10.H1' 3 5.33823 0.136655138213 0.0186746268 
10.H2' 1 4.20449 
10.H5 3 5.27571333333 0.231624986634 0.0536501344333 
10.H6 5 7.49485 0.0285124165935 0.0008129579 

这是读取生成此文件(我从一个文本文件读取生成这些值)的代码

df = pd.read_csv(expAtoms, sep = ' ', header = None) 
df.columns = ["Atom","ppm"] 
gb = (df.groupby("Atom", as_index=False).agg({"ppm":["count","mean","std","var"]}).rename(columns={"count":"nVa", "mean":"avgppm","std":"stddev","var":"delta"})) 

gb.head() 

gb.columns = gb.columns.droplevel() 
gb = gb.rename(columns={"":"Atom"}) 

gb.to_csv("output.txt", sep =" ", index=False) 

在我nVa列和我avgppm柱之间,我想插入叫predppm另一列。我想从一个名为file.txt文件看起来像这样得到的数值:

5.H6 7.72158 0.3 
6.H6 7.70272 0.3 
7.H8 8.16859 0.3 
1.H1' 7.65014 0.3 
9.H8 8.1053 0.3 
10.H6 7.5231 0.3 

我如何检查是否在file.txt第一列中的值=第一列的output.txt,如果它的价值,将第二列file.txt的值插入到我的输出文件中nVa列和avgppm列之间的列中?

例如,1.H1'是在output.txt的和file.txt的,所以我想创建一个在我output.txt的文件称为predppm柱和具有值7.65014(它来自的file.txt的第二列)插入为1.H1'原子。

我想我明白如何添加列,但仅限于可以用于groupby的函数,但我不知道如何在输出中插入任意列。

回答

1

最简单的方法是在pandas.DataFrame上制作index。熊猫有很好的匹配索引的逻辑。

from io import StringIO 
import pandas as pd 

# if python2, do: 
# data = u"""\ 
data = """\ 
Atom nVa avgppm stddev delta 
1.H1' 2 5.73649 0.00104651803616 1.0952e-06 
1.H2' 1 4.85438 
1.H8 1 8.05367 
10.H1' 3 5.33823 0.136655138213 0.0186746268 
10.H2' 1 4.20449 
10.H5 3 5.27571333333 0.231624986634 0.0536501344333 
10.H6 5 7.49485 0.0285124165935 0.0008129579 
""" 

# if python2, do: 
# other_data = u"""\ 
other_data = """\ 
5.H6 7.72158 0.3 
6.H6 7.70272 0.3 
7.H8 8.16859 0.3 
1.H1' 7.65014 0.3 
9.H8 8.1053 0.3 
10.H6 7.5231 0.3 
""" 

# setup these strings so they can be read by pd.read_csv 
# (not necessary if these are actual files on disk) 
data_file = StringIO(data) 
other_data_file = StringIO(other_data) 

# don't say header=None because the first row has the column names 
df = pd.read_csv(data_file, sep=' ') 
# set the index to 'Atom' 
df = df.set_index('Atom') 

# header=None because the other_data doesn't have header info 
other_df = pd.read_csv(other_data_file, sep=' ', header=None) 
# set the column names since they're not specified in other_data 
other_df.columns = ['Atom', 'predppm', 'some_other_field'] 
# set the index to 'Atom' 
other_df = other_df.set_index('Atom') 

# this will assign other_df['predppm'] to the correct rows, 
# because pandas uses the index when assigning new columns 
df['predppm'] = other_df['predppm'] 

print(df) 
#   nVa avgppm stddev  delta predppm 
# Atom            
# 1.H1'  2 5.736490 0.001047 0.000001 7.65014 
# 1.H2'  1 4.854380  NaN  NaN  NaN 
# 1.H8  1 8.053670  NaN  NaN  NaN 
# 10.H1' 3 5.338230 0.136655 0.018675  NaN 
# 10.H2' 1 4.204490  NaN  NaN  NaN 
# 10.H5  3 5.275713 0.231625 0.053650  NaN 
# 10.H6  5 7.494850 0.028512 0.000813 7.52310 

# if you want to return 'Atom' to being a column: 
df = df.reset_index() 
+0

现在我得到一个错误,说'TypeError:initial_value必须是unicode或None,不是str'。我为我的文本文件做了一个变量,所以 'output = output.txt',然后做了'data_file = StringIO(output)',这是我得到的错误 – user8290579

+0

对不起,我的答案在python3中有效。使其在Python 2中工作,看到我上面的新评论 – Hazzles

+0

对不起,我只是有点困惑。你是说'data'和'other_data'是代码本身定义的字符串?但对于我'data'和'other_data'是我正在阅读的文本文件。将'data_file = StringIO(output)'放在哪里'output =“output.txt”'是否会出错?我对实际输入的内容感到困惑,对不起! – user8290579

相关问题