使用concat

时间:2016-06-22 00:55:54

标签: python pandas

运行以下代码后,index.names会返回[None],但我期待['Server']。你能复制一下吗?这是预期的行为,如果是这样的原因?

import pandas as pd
try:
  from io import StringIO
except ImportError:
  from StringIO import StringIO

data1 = "Cores\tServer\n20,000\tS000\n-20,000\tS003\n16,000\tS140\n2,000\tS148\n2,000\tS149\n"

data2 = "Cores\tServer\n20,000\tS103\n16,000\tS140\n2,000\tS148\n2,000\tS149\n4,000\tS150\n"

df1 = pd.read_csv(StringIO(data1), sep='\t', index_col=['Server'], decimal=',')
df2 = pd.read_csv(StringIO(data2), sep='\t', index_col=['Server'], decimal=',')

df1.rename(columns=lambda x: x + '_1', inplace=True)
df2.rename(columns=lambda x: x + '_2', inplace=True)

joined = pd.concat([df1, df2], axis=1)

print(df1)
print(df2)
print(joined)
print(joined.index.name)
print(joined.index.names)

1 个答案:

答案 0 :(得分:1)

我看到你有一个issue - 我认为这可能是一个错误,这是一个解决方法:

In [93]: df1.join(df2, how='outer')
Out[93]: 
        Cores_1  Cores_2
Server                  
S000       20.0      NaN
S003      -20.0      NaN
S103        NaN     20.0
S140       16.0     16.0
S148        2.0      2.0
S149        2.0      2.0
S150        NaN      4.0