将两个值合并到数据帧中的一个单元格中

时间:2018-02-16 18:15:28

标签: python python-3.x pandas dataframe

说我有这样的数据框:

OrderablePartitioner<TSource>

另一个具有百分比值的人:

   minute   values
0   1        3
1   2        4
2   1        1
3   4        6

如何将两个数据帧融合在一起,其中值(计数和百分比)在同一个单元格中进行比较,如此...

   minute   values
0   1        .30
1   2        .40
2   1        .10
3   4        .60

我正在使用Python3和pandas。提前谢谢!

2 个答案:

答案 0 :(得分:4)

pd.concatkeys参数结合使用:

df = pd.concat([df1, df2], axis=1, keys=['Count', 'Percentage'])
df

   Count        Percentage       
  minute values     minute values
0      1      3          1    0.3
1      2      4          2    0.4
2      1      1          1    0.1
3      4      6          4    0.6

然后你可以叠加

df.stack(0)

              minute  values
0 Count            1     3.0
  Percentage       1     0.3
1 Count            2     4.0
  Percentage       2     0.4
2 Count            1     1.0
  Percentage       1     0.1
3 Count            4     6.0
  Percentage       4     0.6

答案 1 :(得分:3)

只需要using System; using System.Collections.Specialized; using System.Net; namespace ConsoleApp18 { public class NoRedirectWebClient : WebClient { protected override WebRequest GetWebRequest(Uri address) { var temp = base.GetWebRequest(address) as HttpWebRequest; temp.AllowAutoRedirect = false; return temp; } } class Program { static void Main(string[] args) { MakeRequest(new WebClient());//Prints the AboutHeader Console.WriteLine(); MakeRequest(new NoRedirectWebClient());//Prints the IndexHeader Console.ReadLine(); } private static void MakeRequest(WebClient webClient) { var loginUrl = @"http://localhost:50900/Home/Login"; NameValueCollection formData = new NameValueCollection(); formData["username"] = "batman"; formData["password"] = "1234"; webClient.UploadValues(loginUrl, "POST", formData); string allheaders = ""; for (int i = 0; i < webClient.ResponseHeaders.Count; i++) allheaders += Environment.NewLine + webClient.ResponseHeaders.GetKey(i) + " = " + webClient.ResponseHeaders.Get(i); Console.WriteLine("******"+webClient.GetType().FullName+"*******"); Console.Write(allheaders); } } } ,然后concatsort_index

set_index